60

For example, there is a string val s = "Test". How do you separate it into t, e, s, t?

aioobe
  • 413,195
  • 112
  • 811
  • 826
sam
  • 2,093
  • 3
  • 17
  • 10

4 Answers4

71

Do you need characters?

"Test".toList    // Makes a list of characters
"Test".toArray   // Makes an array of characters

Do you need bytes?

"Test".getBytes  // Java provides this

Do you need strings?

"Test".map(_.toString)    // Vector of strings
"Test".sliding(1).toList  // List of strings
"Test".sliding(1).toArray // Array of strings

Do you need UTF-32 code points? Okay, that's a tougher one.

def UTF32point(s: String, idx: Int = 0, found: List[Int] = Nil): List[Int] = {
  if (idx >= s.length) found.reverse
  else {
    val point = s.codePointAt(idx)
    UTF32point(s, idx + java.lang.Character.charCount(point), point :: found)
  }
}
UTF32point("Test")
Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
  • 3
    For even being aware of code points, you deserve to have the accepted answer here. Though I doubt the OP will appreciate the subtlety. – Kevin Wright Feb 20 '11 at 13:13
61

You can use toList as follows:

scala> s.toList         
res1: List[Char] = List(T, e, s, t)

If you want an array, you can use toArray

scala> s.toArray
res2: Array[Char] = Array(T, e, s, t)
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 6
    For those seeing this, toCharArray should always be preferred when possible. toList is painfully slow. Each character primitive becomes a Character object. Each link in the list is an object as well. 2 bytes becomes 12+2+12+2=28 bytes. We can no longer have quick and random access. However, if you are just playing around or writing Hello World, then have at it, but don't expect it to scale well. – ldmtwo Jul 23 '14 at 20:22
  • I am new to Scala. Why does `"abc".toList` work but `"abc".toList()` doesn't work. Where can I find documentation for scala String. – zyxue Aug 17 '16 at 21:58
  • 2
    Found the answer here, http://stackoverflow.com/questions/6643030/what-is-the-rule-for-parenthesis-in-scala-method-invocation, never mind. – zyxue Aug 17 '16 at 22:00
6

Actually you don't need to do anything special. There is already implicit conversion in Predef to WrappedString and WrappedString extends IndexedSeq[Char] so you have all goodies that available in it, like:

"Test" foreach println
"Test" map (_ + "!") 

Edit

Predef has augmentString conversion that has higher priority than wrapString in LowPriorityImplicits. So String end up being StringLike[String], that is also Seq of chars.

tenshi
  • 26,268
  • 8
  • 76
  • 90
  • I think it goes via `StringOps` actually. – Kevin Wright Feb 19 '11 at 17:09
  • @Kevin Wright: You are right. `Predef` has `augmentString` conversion that has higher priority than `wrapString` in `LowPriorityImplicits`. Sorry for this, I don't noticed it. Thanks! – tenshi Feb 19 '11 at 17:18
5

Additionally, it should be noted that if what you actually want isn't an actual list object, but simply to do something which each character, then Strings can be used as iterable collections of characters in Scala

for(ch<-"Test") println("_" + ch + "_") //prints each letter on a different line, surrounded by underscores
Dave Griffith
  • 20,435
  • 3
  • 55
  • 76