Groovy String.trim
Remove leading and trailing characters whose value is at most U+0020.
A simple example
String.trim
- Start with " A "
- Do this Remove surrounding spaces
- Result "A"
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.groovyRead 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.
println "[" + " LAB ".trim() + "]"
[LAB]
The ASCII padding is removed.
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.
println "\u2003A\u2003".trim().length()
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.
What to watch for
Use strip for Java whitespace beyond the older trim range. Interior spaces are preserved.