Library contents

Scala String.stripLeading

Remove leading spaces while preserving spacing at the other edge.

A simple example

String.stripLeading

  1. Start with " A "
  2. Do this Remove leading spaces
  3. Result A then two spaces
Only the spaces before A disappear.
Lesson.scala
object Lesson {
    def main(args: Array[String]): Unit = {
        val value = "  A  "
        println("[" + value.stripLeading() + "]")
    }
}
Output
[A  ]

Syntax

Scala syntax
"[" + value.stripLeading() + "]"
Run these examples in Scala

Save one example at a time as Lesson.scala. Use Scala 2.13 on JDK 21.

scalac Lesson.scala
scala Lesson

Align a pasted seat label

A user pastes a multi-line block from a formatted document into a plain-text field. Remove the common outer margin while preserving meaningful differences between the lines inside it.

From the programval value = " A-17 "
Result[A-17 ]
Lesson.scala
object Lesson {
    def main(args: Array[String]): Unit = {
        val value = "  A-17 "
        println("[" + value.stripLeading() + "]")
    }
}
Output
[A-17 ]

The leading spaces vanish and the reserved space after 17 remains.

Advertisements

Clean a copied delivery field

A user pastes a name or code from another application. Remove the unwanted outer padding while preserving the spaces and letters that belong to the actual value.

From the programval value = "\tNorth Road"
Result[North Road]
Lesson.scala
object Lesson {
    def main(args: Array[String]): Unit = {
        val value = "\tNorth Road"
        println("[" + value.stripLeading() + "]")
    }
}
Output
[North Road]

The tab at the beginning is removed.

Clean a course import

A course name arrives padded on both sides. Remove only the leading spaces. Brackets make any spaces left at the end visible.

Given' Art '
Worked result[Art ]

What to watch for

The brackets in the output make significant trailing spaces visible.

Documentation for String.stripLeading