Specific usage of mkString in Scala

Time:2019-10-7

1. Use of mkString () method:

  • The mkString (seq: String) method is to segment the original string using a specific string seq.
  • The mkString (statrt: String, seq: String, end: String) method is to segment the original string using a specific string seq, at the same time, add the string start before the original string, and then add the string end.

object Test {
 def main(args: Array[String]): Unit = {
 var name : String = "Hello LittleLawson"
 var age :Int = 2
 println(name.mkString)
 println(name.mkString(" "))//separate string with space

 var str0 = "scala"
 println(str0.mkString(","))//separate string with comma

 println(str0.mkString("begin",",","end"))

 /*
 1.mkString is used in the inner List,That is say ,Elements in the list is applied.
  */
 val a = List(1,2,3,4)
 val b = new StringBuilder()
 println(a.mkString("List(" , ", " , ")"))

 }
}

The results are as follows:
Hello LittleLawson
H e l l o   L i t t l e L a w s o n
s,c,a,l,a
begins,c,a,l,aend
List(1, 2, 3, 4)

Process finished with exit code 0

2. Scala transforms a collection into a string by mkString method

Problem
If you want to convert a collection element into a string, you may also add separators, prefixes, and suffixes.

Solution
Using the mkString method to print a collection content, here is a simple example:


scala> val a = Array("apple", "banana", "cherry")
a: Array[String] = Array(apple, banana, cherry)

scala> a.mkString
res3: String = applebananacherry

Using the mkString method, you will see that the result is not beautiful. Let’s add a separator:


scala> a.mkString(",")
res4: String = apple,banana,cherry

scala> a.mkString(" ")
res5: String = apple banana cherry

This will look much better. You can also add a prefix and a suffix:


scala> a.mkString("[", ", ", "]")
res6: String = [apple, banana, cherry]

If you want to convert an escape collection into a string, such as a nested array, you first expand the nested array, and then call the mkString method:


scala> val a = Array(Array("a", "b"), Array("c", "d"))
a: Array[Array[String]] = Array(Array(a, b), Array(c, d))

scala> a.flatten.mkString(",")
res7: String = a,b,c,d

Discussion

You can call the toString method of the collection, but it returns the name of the collection with the information of the collection element:


scala> val v = Vector("apple", "banana", "cherry")
v: scala.collection.immutable.Vector[String] = Vector(apple, banana, cherry)

scala> v.toString
res8: String = Vector(apple, banana, cherry)

The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.

Recommended Today

Detailed explanation of spring cloud ribbon load balancing Shenkeng that cannot be connected through the server name

First, the problem. Take the Eureka cluster and the client calls the service through the ribbon. The ribbon side reports the following exceptions java.net.UnknownHostException: SERVICE-HI java.lang.IllegalStateException: No instances available for SERVICE-HI java.lang.IllegalStateException: Request URI does not contain a valid hostname: http://SERVICE-HI com.netfix.discovery.shared.taransport.TransportException: Cannot execute request on any known server The spring cloud version is messy, […]