使用字符串切分函数split(String)来分割字符串,会返回一个字符串数组。正则表达式
scala> "hello world".split(" ") res16: Array[String] = Array(hello, world)
使用集合操做函数foreach就能够遍历切分后得到的字符串数组。数组
scala> "hello world".split(" ").foreach(println) hello world
一样你也可使用正则表达式做为参数来切分字符串函数
scala> "hello world, this is me!".split("\\s+") res19: Array[String] = Array(hello, world,, this, is, me!)