How to Split a String in Scala

Suraj P Feb 02, 2024
How to Split a String in Scala

This article will introduce the methods for splitting a string in the Scala programming language.

Use the split() Method to Split a String in Scala

Scala provides a method called split(), which is used to split a given string into an array of strings using the delimiter passed as a parameter.

This is optional, but we can also limit the total number of elements of the resultant array using the limit parameter.

Let’s look at an example when we have only one delimiter present in the string.

Syntax:

val resultant_array = string_Name.split(delimiter, limit(optional))

Case 1:

object MyClass
{
    def main(args: Array[String])
    {
        val line = "brick,cement,concrete"

        // Split on each comma.
        val arr = line.split(',')

        arr.foreach(println) //printing the resultant array
    }
}

Output:

brick
cement
concrete

Now, let’s look at a case with multiple delimiters in a string. Inside the split method, we pass all the delimiters in an array.

The split handles the delimiters. Then, the alphabetical strings are returned in the array.

Syntax:

val resultant_array = string_Name.split(Array_containing_delimiters, limit(optional))

Case 2:

object MyClass
{
    def main(args: Array[String])
    {
        val codes = "abc;def,ghi:jkl"
        val arr = codes.split(Array(';', ',', ':')) //all the delimiters are passed in an array
        arr.foreach(println)      
    }
}

Output:

abc
def
ghi
jkl

Let’s look at a case when the delimiter is a string; rather than a single character, and it is a collection of characters.

Syntax:

val resultant_array = string_Name.split(string delimiter, limit(optional))

Case 3:

object MyClass {
    def main(args: Array[String]) {
        val codes = "Tony stark is the iron man he is the hero"

        val arr = codes.split("the") //splitting at every "the"

        arr.foreach(println)
    }
}

Output:

Tony stark is 
 iron man he is 
 hero

In addition, we can use a regex regular expression to separate a string. This is the preferred method as we can write a regex that can handle multiple cases simultaneously.

Case 4:

object MyClass {
    def main(args: Array[String]) {
        val items = "scala; ; python; c++"

        // Any number of spaces or semicolons is a delimiter.
        val arr = items.split("[; ]+")

        //we can see that no empty element or whitespace is printed as regex handles it perfectly
        arr.foreach(println)
    }
}

Output:

scala
python
c++

The empty string between delimiters in items is ignored in the code above. Then, the two surrounding delimiters are combined.

Author: Suraj P
Suraj P avatar Suraj P avatar

A technophile and a Big Data developer by passion. Loves developing advance C++ and Java applications in free time works as SME at Chegg where I help students with there doubts and assignments in the field of Computer Science.

LinkedIn GitHub

Related Article - Scala String