Library contents

Groovy String.copyValueOf

Create a string from a character array or a portion of one.

A simple example

String.copyValueOf

  1. Start with Characters a, b, c
  2. Do this Copy just the middle character
  3. Result b
Starting at b and copying one position leaves a and c outside the result.

Syntax

Groovy syntax
public static String copyValueOf(char[] data, int offset, int count)
Run these examples in Groovy

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

groovy Lesson.groovy

Save digits from a reader

A scanner reuses its input buffer for the next item. Save the current text before that buffer changes so the label or note already accepted by the application keeps its original contents.

From the programchar[] code = "PK12".toCharArray()
Result12
Lesson.groovy
char[] code = "PK12".toCharArray()
println String.copyValueOf(code, 2, 2)
Output
12

The final argument is a count of units.

Advertisements

Copy a complete label

A label reader supplies an entire character array to the application. Save the whole array as display text rather than accidentally selecting only a slice of the scanned label.

From the programchar[] tag = "Tea".toCharArray()
ResultTea
Lesson.groovy
char[] tag = "Tea".toCharArray()
println String.copyValueOf(tag)
Output
Tea

The result contains all units in the array.

Read a parcel sorting code

The depot scans a parcel marked NC-17. Copy the last two characters into a clean buffer for a numeric display. What does the copied field contain?

GivenNC-17
Worked result17

What to watch for

The last range argument is a count. Check that offset plus count fits the array.

Documentation for String.copyValueOf