Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use mkString in Scala

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report