w3resource

Scala Programming: Trim any leading or trailing whitespace from a given string

Scala Programming String Exercise-13 with Solution

Write a Scala program to trim any leading or trailing whitespace from a given string.

Sample Solution:

Scala Code:

object Scala_String {
  def main(args: Array[String]): Unit = {
    {
      val str = " Scala Exercises ";

      // Trim the whitespace from the front and back of the String.
      val new_str = str.trim();
      // Display the strings for comparison.
      println("Original String:-" + str);
      println("New String:-" + new_str);
    }
  }
}

Sample Output:

Original String:- Scala Exercises 
New String:-Scala Exercises

Scala Code Editor :

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

Previous:Write a Scala program to convert all the characters to lowercase, uppercase strings.
Next:Write a Scala program to print after removing duplicates from a given string.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/scala-exercises/string/scala-string-exercise-13.php