Groovy String.codePointBefore
Read the Unicode code point that ends immediately before an offset.
A simple example
String.codePointBefore
- Start with "AB"
- Do this Read the code point before the end
- Result 66
Syntax
public int codePointBefore(int index)
Run these examples in Groovy
Save one example at a time as Lesson.groovy. Use Groovy 4 on JDK 21.
groovy Lesson.groovyRead the last reaction
A status message ends with a Unicode symbol that can occupy several encoding units. Locate the last complete code point before identifying the status that the user actually selected.
String note = "OK😀"
println note.codePointBefore(note.length())
128512
The offset just beyond the text includes both surrogate units.
Find the previous row letter
A compact printed code assigns meaning to particular positions. Locate the correct character for the requested field and check the indexing convention before sending the value to the display.
println "B-7".codePointBefore(1)
66
The preceding B has code point 66.
Read a parcel sorting code
The depot scans a parcel marked NC-17. The scanner reads the code point immediately before the dash. What decimal Unicode value does it find?
What to watch for
The offset must be positive and may equal the string length.