Library contents

Groovy String.trim

Remove leading and trailing characters whose value is at most U+0020.

A simple example

String.trim

  1. Start with " A "
  2. Do this Remove surrounding spaces
  3. Result "A"
The middle letter stays in place while both ends lose their spaces.

Syntax

Groovy syntax
public String trim()
Run these examples in Groovy

Save one example at a time as Lesson.groovy. Use Groovy 4 on JDK 21.

groovy Lesson.groovy

Read an old fixed-width label

A legacy scanner pads a printed label to a fixed width using ordinary spaces. Remove that padding before comparing the value with the shorter name in the current catalog.

Result[LAB]
Lesson.groovy
println "[" + "  LAB  ".trim() + "]"
Output
[LAB]

The ASCII padding is removed.

Advertisements

Recognize a Unicode margin

An older import includes padding around a fixed-width label. Compare the characters actually removed by the chosen trimming operation, since a visible margin can contain more than an ordinary space.

Result3
Lesson.groovy
println "\u2003A\u2003".trim().length()
Output
3

The original Java trim range does not include em space.

Clean a course import

A course name arrives padded on both sides. Remove leading and trailing ASCII spaces before placing the text between the display brackets.

Given' Art '
Worked result[Art]

What to watch for

Use strip for Java whitespace beyond the older trim range. Interior spaces are preserved.

Documentation for String.trim