Library contents

Scala String.replaceAll

Replace every run of digits before showing a booking record on a shared display.

A simple example

String.replaceAll

  1. Start with "a1b2"
  2. Do this Replace each digit with *
  3. Result "a*b*"
The digit pattern finds two matches. Both are replaced.

Syntax

Scala syntax
value.replaceAll("[0-9]+", "*")
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

Hide both ticket numbers

A service desk displays an identifier where bystanders could see it. Replace the intended numeric field or digit runs while preserving the rest of the record for the operator.

From the programval value = "A12 / B304"
ResultA* / B*
Lesson.scala
object Lesson {
    def main(args: Array[String]): Unit = {
        val value = "A12 / B304"
        println(value.replaceAll("[0-9]+", "*"))
    }
}
Output
A* / B*

Both runs become one asterisk each.

Advertisements

Leave a public route name alone

A transit editor changes selected text in a route while preserving its remaining stops or punctuation. Identify which occurrences belong to the requested edit before rebuilding the public route label.

From the programval value = "North Gate"
ResultNorth Gate
Lesson.scala
object Lesson {
    def main(args: Array[String]): Unit = {
        val value = "North Gate"
        println(value.replaceAll("[0-9]+", "*"))
    }
}
Output
North Gate

No digits means no substitution.

Mask a public job number

A noticeboard shows the department prefix and hides the serial. The editor replaces a matching run of digits with a single star before sharing the label. Each sample has one numeric run.

GivenART-184 → replace digits
Worked resultART-*

What to watch for

Each run of one or more digits is one match, so its original length is hidden.

Documentation for String.replaceAll