w3resource

Scala Programming: Get a substring of a given string between two specified positions

Scala Programming String Exercise-11 with Solution

Write a Scala program to get a substring of a given string between two specified positions.

Sample Solution:

Scala Code:

object Scala_String {
  def main(args: Array[String]): Unit = {
    val str = "The quick brown fox jumps over the lazy dog.";

    // Get a substring of the above string starting from
    // index 10 and ending at index 26.
    val new_str = str.substring(10, 26);

    // Display the two strings for comparison.
    println("Original = " + str);
    println("From said string, substring between two positions (10,26) = " + new_str);

  }
}

Sample Output:

Original = The quick brown fox jumps over the lazy dog.
From said string, substring between two positions (10,26) = brown fox jumps 

Scala Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous:Write a Scala program to replace a specified character with another character.
Next:Write a Scala program to convert all the characters to lowercase, uppercase strings.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.