How to convert a Scala array (sequence) to string with mkString | alvinalexander.com

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.) I create a new String variable named string, separating each String in the array with a space character, which I specified when calling the mkString function. Note that I could have given the mkString function any String to use as a separating character, like this: As a final example, you can also use the Scala mkString method to convert an Int array to a String, like this: When using mkString with a Scala array, list, seq, etc., you can also define a prefix, suffix, and element separator, as shown in these examples: Those examples show typical prefix, separator, and suffix values, but you can use any String you want for those three values. Source.


Яндекс.Метрика Рейтинг@Mail.ru Free Web Counter
page counter
Last Modified: April 18, 2016 @ 8:08 pm