Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use mkString in Scala

Shulou Source: shulou.com Published: 2022-06-03 19:09:44 11月12日 Update

This article mainly introduces "the specific use of mkString in Scala". In the daily operation, I believe that many people have doubts about the specific use of mkString in Scala. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the specific use of mkString in Scala". Next, please follow the editor to study!

Use of the 1.mkString () method:

The mkString (seq:String) method splits the original string with a specific string seq. The mkString (statrt:String,seq:String,end:String) method splits the original string with a specific string seq, adding the string start before the original string and the string end after it.

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 val b = new StringBuilder () println (a.mkString ("List (", ",")}}

The execution results are as follows: Hello LittleLawsonH ello LittleLawso ns,c,a,l,abegins,c,a,l,aendList (1,2,3,4)

Process finished with exit code 0

2.scala converts a collection into a string through the mkString method.

Problem if you want to convert collection elements to strings, you may also add delimiters, prefixes, and suffixes.

Solution uses the mkString method to print a collection. Here's a simple example:

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

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

Scala > a.mkString (",") res4: String = apple,banana,cherryscala > a.mkString (") res5: String = apple banana cherry

It looks much better this way, and you can also add a prefix and a suffix:

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

If you want to convert an escape set into a string, such as a nested array, 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), Array (c) scala > a.flatten.mkString (",") res7: String = a line bline d

Discussion

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

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

At this point, the study on "the specific use of mkString in Scala" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Methods characters strings usage learning elements delimiters prefixes suffixes arrays more results help good-looking practical beautiful next examples information content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia macOS Huawei Linux Microsoft