A Scala “split string” example

Scala String FAQ: How do I split a String in Scala based on a field separator, such as a string I get from a comma-separated value (CSV) or pipe-delimited file.

Back to top

Solution

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) 
hello
world
Back to top

Real-world example

Here’s a real-world example that shows how to split a URL-encoded string you might receive in a web application:

scala> val result = "oauth_token=FOO&oauth_token_secret=BAR&oauth_expires_in=3600"                                   result: java.lang.String = oauth_token=FOO&oauth_token_secret=BAR&oauth_expires_in=3600

scala> val nameValuePairs = result.split("&")
nameValuePairs: Array[java.lang.String] = Array(oauth_token=FOO, oauth_token_secret=BAR, oauth_expires_in=3600)

As you can see in the second line, I call the split method, telling it to use the & character to split my string into multiple strings. As you can see in the REPL output, the variable nameValuePairs is an Array of String type, and in this case, these are the name/value pairs I wanted.

Back to top

Splitting a CSV string

Here’s an example that shows how to split a CSV string into a string array:

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")

Note that when using this approach it’s best to trim each string. This is shown in the following code, where I 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)

Note: This isn’t a perfect solution because CSV rows can have additional commas inside of quotes. I’m just trying to show how this approach generally works.

Back to top

Splitting with regular expressions

You can also split a string based on a regular expression (regex). 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)

For more examples of regular expressions, see the Java Pattern class, or see my common Java regular expression examples.

Back to top

Where the ‘split’ method comes from

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.)

Note: The actual Scala class that contains the split method may change over time. At the time of this writing the split method is in the StringLike class, but as the Scala libraries are reorganized this may change.

Back to top

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.