Library contents

Groovy String.codePointBefore

Read the Unicode code point that ends immediately before an offset.

A simple example

String.codePointBefore

  1. Start with "AB"
  2. Do this Read the code point before the end
  3. Result 66
The position immediately before the end contains B.

Syntax

Groovy 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.groovy

Read 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.

From the programString note = "OK😀"
Result128512
Lesson.groovy
String note = "OK😀"
println note.codePointBefore(note.length())
Output
128512

The offset just beyond the text includes both surrogate units.

Advertisements

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.

Result66
Lesson.groovy
println "B-7".codePointBefore(1)
Output
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?

GivenNC-17
Worked result67

What to watch for

The offset must be positive and may equal the string length.

Documentation for String.codePointBefore