1.3. Splitting Strings


You want to split a string into parts based on a field separator, such as a string you get from a comma-separated value (CSV) or pipe-delimited file.


Use one of the split methods that are available on String objects:

scala> "hello world".split(" ")
res0: Array[java.lang.String] = Array(hello, world)

The split method returns an array of String elements, which you can then treat as a normal Scala Array:

scala> "hello world".split(" ").foreach(println)


The string that the split method takes can be a regular expression, so you can split a string on simple characters like a comma in a CSV file:

scala> val s = "eggs, milk, butter, Coco Puffs"
s: java.lang.String = eggs, milk, butter, Coco Puffs

// 1st attempt
scala> s.split(",")
res0: Array[java.lang.String] = Array(eggs, " milk", " butter", " Coco Puffs")

Using this approach, it’s best to trim each string. Use the map method to call trim on each string before returning the array:

// 2nd attempt, cleaned up
scala> s.split(",").map(_.trim)
res1: Array[java.lang.String] = Array(eggs, milk, butter, Coco Puffs)

You can also split a string based on a regular expression. This example shows how to split a string on whitespace characters:

scala> "hello world, this is Al".split("\\s+")
res0: Array[java.lang.String] = Array(hello, world,, this, is, Al)

About that split method...

The split method is overloaded, with some versions of the method coming from the Java String class and some coming from the Scala StringLike class. For instance, if you call split with a Char argument instead of a String argument, you’re using the split method from StringLike:

// split with a String argument
scala> "hello world".split(" ")
res0: Array[java.lang.String] = Array(hello, world)

// split with a Char argument
scala> "hello world".split(' ')
res1: Array[String] = Array(hello, world)

The subtle difference in that output—Array[java.lang.String] versus Array[String]—is a hint that something is different, but as a practical matter, this isn’t important. Also, with the Scala IDE project integrated into Eclipse, you can see where each method comes from when the Eclipse “code assist” dialog is displayed. (IntelliJ IDEA and NetBeans may show similar information.)

Get Scala Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.