Scala program to split a string by a delimiter

1. Introduction

Splitting a string by a specific delimiter is a common operation in text processing and data manipulation. In Scala, the built-in String methods make this task straightforward. This post demonstrates how to split a string using a particular delimiter in Scala.

2. Program Steps

1. Set up the Scala environment.

2. Define a string and a delimiter.

3. Use the split method available on Scala's String class to divide the string based on the delimiter.

4. Print the split results.

5. Execute the program.

3. Code Program

object StringSplitApp {
  def main(args: Array[String]): Unit = {
    // Original string and delimiter
    val str = "Hello,World,Scala,Programming"
    val delimiter = ","

    // Splitting the string
    val splitResult = splitStringByDelimiter(str, delimiter)

    println(s"Original String: $str")
    println(s"Split Result: ${splitResult.mkString("[", ", ", "]")}")

  def splitStringByDelimiter(input: String, delimiter: String): Array[String] = {


Original String: Hello,World,Scala,Programming
Split Result: [Hello, World, Scala, Programming]


1. The StringSplitApp object houses the primary logic of our application.

2. The main function begins by defining an example str that contains multiple words separated by a comma (,).

3. We then set our delimiter as a comma.

4. The function splitStringByDelimiter is a simple utility method that accepts the input string and the delimiter as arguments and returns an array of substrings. This function leverages Scala's built-in split method.

5. To display the result in a user-friendly manner, we use mkString to print the split words within brackets.