/home/wpollock1/public_html/AJava/CodePointDemo.java
// This class shows how to work with I18n strings, since not
// all Unicode characters (code points) can be stored in a Java
// "char" (or code unit). So to iterate over the characters in
// a string the old code of:
// for ( int i=0; i<str.length(); ++i) {
// char c = str.charAt(i); ...;
// }
// needs to be converted to something like the for loop shown below.
//
// Written 1/2010 by Wayne Pollock, Tampa Florida USA
import static java.lang.Character.toChars;
import static java.lang.Integer.toHexString;
import static java.lang.String.*;
import static java.lang.System.*;
public class CodePointDemo
{
public static void main ( String [] args )
{
// If user supplies a string (word), use it; else
// use a sample string containing weird Unicode:
String str = args != null && args.length>0 ?
args[0] : "A G-clef: \"\uD834\uDD1E\".";
// For each character, do something:
for ( int i=0; i<str.length(); i = str.offsetByCodePoints(i, 1) ) {
int cp = str.codePointAt(i);
// To demo how, show the index, the char (may not display
// correctly without an appropriate font), and the hex values:
out.println( i + ": \"" + new String(toChars(cp))
+ "\" (0x" + toHexString(cp) + ")" );
}
}
}