Character and String APIs
Character class encapsulates the
char data type. For the J2SE release 5, many methods were added to the
Character class to support supplementary characters. This API falls into two categories: methods that convert between
char and code point values and methods that verifiy the validity of or map code points.
This section describes a subset of the available methods in the
Character class. For the complete list of available APIs, see the Character class specification.
The following table includes the most useful conversion methods, or methods that facilitate conversion, in the
Character class. The
codePointBefore methods are included in this list because text is generally found in a sequence, such as a
String , and these methods can be used to extract the desired substring.
|toChars(int codePoint, char dst, int dstIndex)|
|Converts the specified Unicode code point to its UTF-16 representation and places it in a |
|toCodePoint(char high, char low)|
toCodePoint(char, int, int)
|Converts the specified parameters to its supplementary code point value. The different methods accept different input formats.|
|codePointAt(char a, int index)|
codePointAt(char a, int index, int limit)
codePointAt(CharSequence seq, int index)
|Returns the Unicode code point at the specified index. The third method takes a |
|codePointBefore(char a, int index)|
codePointBefore(char a, int index, int start)
codePointBefore(CharSequence seq, int index)
codePointBefore(char, int, int)
|Returns the Unicode code point before the specified index. The third method accepts a |
|charCount(int codePoint)||Returns the value 1 for characters that can be represented by a single |
Some of the previous methods that used the
char primitive data type, such as
isDigit(char) , were supplanted by methods that support supplementary characters, such as
isDigit(int) . The previous methods are supported but do not work with supplementary characters. To create a global application and ensure that your code works seamlessly with any language, it is recommended that you use the newer forms of these methods.
Note that, for performance reasons, most methods that accept a code point do not verify the validity of the code point parameter. You can use the
isValidCodePoint method for that purpose.
The following table lists some of the verification and mapping methods in the
|isValidCodePoint(int codePoint)||Returns true if the code point is within the range of 0x0000 to 0x10FFFF, inclusive.|
|isSupplementaryCodePoint(int codePoint)||Returns true if the code point is within the range of 0x10000 to 0x10FFFF, inclusive.|
|isHighSurrogate(char)||Returns true if the specified |
|isLowSurrogate(char)||Returns true if the specified |
|isSurrogatePair(char high, char low)||Returns true if the specified high and low surrogate code values represent a valid surrogate pair.|
|codePointCount(CharSequence, int, int)|
codePointCount(char, int, int)
|Returns the number of Unicode code points in the |
|Returns true if the specified Unicode code point is a lowercase or uppercase character.|
|isDefined(int)||Returns true if the specified Unicode code point is defined in the Unicode standard.|
|Returns true if the specified character or Unicode code point is permissible as the first character in a Java identifier.|
|Returns true if the specified Unicode code point is a letter, a digit, or a letter or digit.|
|getDirectionality(int)||Returns the Unicode directionality property for the given Unicode code point.|
|Character.UnicodeBlock.of(int codePoint)||Returns the object representing the Unicode block that contains the given Unicode code point or returns |
StringBuffer , and
StringBuilder classes also have contructors and methods that work with supplementary characters. The following table lists some of the commonly used methods. For the complete list of available APIs, see the javadoc for the String, StringBuffer, and StringBuilder classes.