How to convert a Scala array (sequence) to string with mkString | alvinalexander.com
By Alvin Alexander. Last updated: Jun 9, 2014 Scala collections FAQ: How can I convert a Scala array to a String? (Or, more, accurately, how do I convert any Scala sequence to a String.) A simple way to convert a Scala array to a String is with the mkString method of the Array class . (Although I've written "array", the same technique also works with any Scala sequence, including Array, List, Seq, ArrayBuffer, Vector, and other sequence types.) Here's a quick array to string example using the Scala REPL: scala> val args = Array("Hello", "world", "it's", "me") args: Array[java.lang.String] = Array(Hello, world, it's, me) scala> val string = args.mkString(" ") string: String = Hello world it's me In this first statement: val args = Array("Hello", "world", "it's", "me") I create a Scala array named args, and in this second statement: val string = args.mkString(" ") I create a new String variable named string, separating each String in the array with a space character,Read full article from How to convert a Scala array (sequence) to string with mkString | alvinalexander.com
No comments:
Post a Comment