Scala學習之字符串篇(三):字符串的切分

使用字符串切分函數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!)
相關文章
相關標籤/搜索