Java Reference
In-Depth Information
tunately, there is a go-between standard, called UTF-16 (16-bit Unicode Transformation
Format). As the String class documentation puts it:
A String represents a string in the UTF-16 format in which supplementary characters are rep-
resented by surrogate pairs (see the section Unicode Character Representations in the Charac-
ter class for more information). Index values refer to char code units, so a supplementary char-
acter uses two positions in a String.
The String class provides methods for dealing with Unicode code points (i.e., characters), in ad-
dition to those for dealing with Unicode code units (i.e., char values).
The charAt() method of String returns the char value for the character at the specified off-
set. The StringBuilder append() method has a form that accepts a char . Because char is
an integer type, you can even do arithmetic on char s, though this is not needed as frequently
as in, say, C. Nor is it often recommended, because the Character class provides the meth-
ods for which these operations were normally used in languages such as C. Here is a program
that uses arithmetic on char s to control a loop, and also appends the characters into a
StringBuilder (see Putting Strings Together with StringBuilder ) :
// UnicodeChars.java
StringBuffer b = new
new StringBuffer ();
for
for ( char
char c = 'a' ; c < 'd' ; c ++) {
b . append ( c );
}
b . append ( '\u00a5' );
// Japanese Yen symbol
b . append ( '\u01FC' );
// Roman AE with acute accent
b . append ( '\u0391' );
// GREEK Capital Alpha
b . append ( '\u03A9' );
// GREEK Capital Omega
for
for ( int
int i = 0 ; i < b . length (); i ++) {
System . out . printf (
"Character #%d (%04x) is %c%n" ,
i , ( int
int ) b . charAt ( i ), b . charAt ( i ));
}
System . out . println ( "Accumulated characters are " + b );
When you run it, the expected results are printed for the ASCII characters. On my Unix sys-
tem, the default fonts don't include all the additional characters, so they are either omitted or
mapped to irregular characters ( Drawing Text shows how to draw text in other fonts):
Search WWH ::




Custom Search