Java Reference
In-Depth Information
13.1. Character Sequences
As described in " Character Set " on page 161 , the Java programming lan-
guage represents text consisting of Unicode characters as sequences of
char values using the UTF -16 encoding format. The String class defines
objects that represent such character sequences. More generally, the
java.lang.CharSequence interface is implemented by any class that rep-
resents such a character sequencethis includes the String , StringBuilder ,
and StringBuffer classes described in this chapter, together with the
java.nio.CharBuffer class that is used for performing I/O.
The CharSequence interface is simple, defining only four methods:
public char charAt(int index)
Returns the char in this sequence at the given index . Sequences
are indexed from zero to length()-1 (just as arrays are in-
dexed). As this is a UTF -16 sequence of characters, the re-
turned value may be an actual character or a value that is part
of a surrogate pair. If the index is negative or not less than
the length of the sequence, then an IndexOutOfBoundsException
is thrown.
public int length()
Returns the length of this character sequence.
public CharSequence subSequence(int start, int end)
Returns a new CharSequence that contains the char values in this
sequence consisting of charAt(start) through to charAt(end-1) .
If start is less than end or if use of either value would try to
index outside this sequence, then an IndexOutOfBoundsException
is thrown. Be careful to ensure that the specified range doesn't
split any surrogate pairs.
 
Search WWH ::




Custom Search