Home
Scala
String Strip: stripLineEnd and stripMargin
Updated Dec 15, 2023
Dot Net Perls
Strip. Often strings have surrounding whitespace. A string may have a trailing newline. It may have a margin (with a separator) at its start.
With special strip functions in the Scala language we process these whitespace sequences. We eliminate them. We handle prefixes and suffixes.
StripLineEnd. This program invokes the stripLineEnd def. It removes a trailing newline from a string. It is similar to "chop" or "chomp" in scripting languages.
object Program { def main(args: Array[String]): Unit = { val name = "Thomas Browne\n" // Remove end newline from the string. val result = name.stripLineEnd // Print the result. println(s"[$result]") } }
[Thomas Browne]
StripMargin. Often in text processing a string has a "start" character that ends its leading margin. Whitespace chars (like spaces) may precede this.
Here With stripMargin we handle this case. We eliminate the entire margin (leading blanks and separator) from the strings in a list.
object Program { def main(args: Array[String]): Unit = { // These strings have margins. val lines = List(" :cat", " :dog") for (line <- lines) { // Remove all blanks and a separator character at start of string. val result = line.stripMargin(':') // Print results. println(s"[$line] $result") } } }
[ :cat] cat [ :dog] dog
StripMargin, no argument. We can use stripMargin with no argument. This removes a vertical bar from the beginning of the string (along with the spaces and whitespace).
object Program { def main(args: Array[String]): Unit = { val data = " |Example" // With this version of stripMargin, the vertical bar is removed. val result = data.stripMargin // Print the result. println(s"[$result]") } }
[Example]
StripPrefix, stripSuffix. These methods are similar to startsWith and endsWith, but they also remove the starting and ending substrings.
Info If a string's start or end matches the argument, that part is removed. Otherwise the string is returned with no changes.
// Has a prefix and a suffix (content is "cat"). object Program { def main(args: Array[String]): Unit = { val input = "x:cat:end" // Remove prefix. val prefixRemoved = input.stripPrefix("x:") // Remove suffix. val suffixRemoved = input.stripSuffix(":end") // Remove prefix and suffix. val bothRemoved = input.stripPrefix("x:").stripSuffix(":end") // Print stripped strings. println(prefixRemoved) println(suffixRemoved) println(bothRemoved) } }
cat:end x:cat cat
Summary. With the strip methods in Scala, we clear unwanted characters (like newlines, spaces) from strings. This is a useful and often-needed feature.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Dec 15, 2023 (edit).
Home
Changes
© 2007-2025 Sam Allen