Standalone utility class providing UTF16 character conversions and indexing conversions.
Code that uses strings alone rarely need modification. By design, UTF-16 does not allow overlap,
so searching for strings is a safe operation. Similarly, concatenation is always safe.
Substringing is safe if the start and end are both on UTF-32 boundaries. In normal code, the
values for start and end are on those boundaries, since they arose from operations like
searching. If not, the nearest UTF-32 boundaries can be determined using bounds().
The following examples illustrate use of some of these methods.
// iteration forwards: Original
for (int i = 0; i < s.length(); ++i) {
char ch = s.charAt(i);
doSomethingWith(ch);
}
// iteration forwards: Changes for UTF-32
int ch;
for (int i = 0; i < s.length(); i += UTF16.getCharCount(ch)) {
ch = UTF16.charAt(s, i);
doSomethingWith(ch);
}
// iteration backwards: Original
for (int i = s.length() - 1; i >= 0; --i) {
char ch = s.charAt(i);
doSomethingWith(ch);
}
// iteration backwards: Changes for UTF-32
int ch;
for (int i = s.length() - 1; i > 0; i -= UTF16.getCharCount(ch)) {
ch = UTF16.charAt(s, i);
doSomethingWith(ch);
}
Notes:
Lead
and Trail in the API, which gives a better sense of their ordering in a string.
offset16 and offset32 are used to distinguish offsets to UTF-16
boundaries vs offsets to UTF-32 boundaries. int char32 is used to contain UTF-32
characters, as opposed to char16, which is a UTF-16 code unit. bounds(string, offset16) != TRAIL.
UCharacter.isLegal() can be used to
check for validity if desired. | Modifier and Type | Class and Description |
|---|---|
| public static class | UTF16.StringComparator
UTF16 string comparator class. |
| Modifier and Type | Field and Description |
|---|---|
| public static final int | CODEPOINT_MAX_VALUE
The highest Unicode code point value (scalar value) according to the Unicode Standard. |
| public static final int | CODEPOINT_MIN_VALUE
The lowest Unicode code point value. |
| private static final int | LEAD_SURROGATE_BITMASK
Lead surrogate bitmask |
| private static final int | LEAD_SURROGATE_BITS
Lead surrogate bits |
| public static final int | LEAD_SURROGATE_BOUNDARY
Value returned in |
| public static final int | LEAD_SURROGATE_MAX_VALUE
Lead surrogate maximum value |
| public static final int | LEAD_SURROGATE_MIN_VALUE
Lead surrogate minimum value |
| private static final int | LEAD_SURROGATE_OFFSET_
Value that all lead surrogate starts with |
| private static final int | LEAD_SURROGATE_SHIFT_
Shift value for lead surrogate to form a supplementary character. |
| public static final int | SINGLE_CHAR_BOUNDARY
Value returned in |
| public static final int | SUPPLEMENTARY_MIN_VALUE
The minimum value for Supplementary code points |
| private static final int | SURROGATE_BITMASK
Surrogate bitmask |
| private static final int | SURROGATE_BITS
Surrogate bits |
| public static final int | SURROGATE_MAX_VALUE
Maximum surrogate value |
| public static final int | SURROGATE_MIN_VALUE
Surrogate minimum value |
| private static final int | TRAIL_SURROGATE_BITMASK
Trail surrogate bitmask |
| private static final int | TRAIL_SURROGATE_BITS
Trail surrogate bits |
| public static final int | TRAIL_SURROGATE_BOUNDARY
Value returned in |
| private static final int | TRAIL_SURROGATE_MASK_
Mask to retrieve the significant value from a trail surrogate. |
| public static final int | TRAIL_SURROGATE_MAX_VALUE
Trail surrogate maximum value |
| public static final int | TRAIL_SURROGATE_MIN_VALUE
Trail surrogate minimum value |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private static int | |
| private static int | |
| public static StringBuffer | Returns: the updated StringBufferThe buffer to append to target, int Value to append. char32)Append a single UTF-32 value to the end of a StringBuffer. |
| public static int | Returns: offset after char32 in the array.Char array to be append with the new code point target, int UTF16 offset which the codepoint will be appended. limit, int Code point to be appended char32)Adds a codepoint to offset16 position of the argument char array. |
| public static StringBuffer | Returns: the updated StringBufferThe buffer to append to target, int The code point to append cp)Cover JDK 1.5 APIs. |
| public static int | Returns:
Text to analyse source, int UTF-16 offset offset16)Returns the type of the boundaries around the char at offset16. |
| public static int | Returns:
String buffer to analyse source, int UTF16 offset offset16)Returns the type of the boundaries around the char at offset16. |
| public static int | Returns:
Char array to analyse source, int Offset to substring in the source array for analyzing start, int Offset to substring in the source array for analyzing limit, int UTF16 offset relative to start offset16)Returns the type of the boundaries around the char at offset16. |
| public static int | |
| public static int | Returns: UTF-32 value for the UTF-32 value that contains the char at offset16. The boundaries of that codepoint are the same as inbounds32().Array of UTF-16 chars source, int UTF-16 offset to the start of the character. offset16)Extract a single UTF-32 value from a string. |
| public static int | Returns: UTF-32 value for the UTF-32 value that contains the char at offset16. The boundaries of that codepoint are the same as inbounds32().UTF-16 chars string buffer source, int UTF-16 offset to the start of the character. offset16)Extract a single UTF-32 value from a string. |
| public static int | Returns: UTF-32 value for the UTF-32 value that contains the char at offset16. The boundaries of that codepoint are the same as inbounds32().Array of UTF-16 chars source, int Offset to substring in the source array for analyzing start, int Offset to substring in the source array for analyzing limit, int UTF-16 offset relative to start offset16)Extract a single UTF-32 value from a substring. |
| public static int | Returns: UTF-32 value for the UTF-32 value that contains the char at offset16. The boundaries of that codepoint are the same as inbounds32().UTF-16 chars string buffer source, int UTF-16 offset to the start of the character. offset16)Extract a single UTF-32 value from a string. |
| public static int | Returns: equivalent of code point comparator comparing two strings.to test codePoint, CharSequence to test s)Utility for comparing a code point to a string without having to create a new string. |
| public static int | Returns: number of codepoint in stringUTF16 string source)Number of codepoints in a UTF16 String |
| public static int | Returns: number of codepoint in stringUTF16 string buffer source)Number of codepoints in a UTF16 String buffer |
| public static int | Returns: number of codepoint in the substringUTF16 char array source, int Offset of the substring start, int Offset of the substring limit)Number of codepoints in a UTF16 char array substring |
| public static StringBuffer | Returns: a reference to targetString buffer to remove codepoint from target, int Offset which the codepoint will be removed offset16)Removes the codepoint at the specified position in this target (shortening target by 1 character if the codepoint is a non-supplementary, 2 otherwise). |
| public static int | Returns: a new limit sizeString buffer to remove codepoint from target, int End index of the char array, limit <= target.length limit, int Offset which the codepoint will be removed offset16)Removes the codepoint at the specified position in this target (shortening target by 1 character if the codepoint is a non-supplementary, 2 otherwise). |
| public static int | Returns: UTF-32 offsetText to analyse source, int UTF-16 offset < source text length. offset16)Returns the UTF-32 offset corresponding to the first UTF-32 boundary at or after the given UTF-16 offset. |
| public static int | Returns: UTF-32 offsetText to analyse source, int UTF-16 offset < source text length. offset16)Returns the UTF-32 offset corresponding to the first UTF-32 boundary at the given UTF-16 offset. |
| public static int | Returns: UTF-32 offset relative to startText to analyse source, int Offset of the substring start, int Offset of the substring limit, int UTF-16 relative to start offset16)Returns the UTF-32 offset corresponding to the first UTF-32 boundary at the given UTF-16 offset. |
| public static int | Returns: UTF-16 offsetThe UTF-16 string source, int UTF-32 offset offset32)Returns the UTF-16 offset that corresponds to a UTF-32 offset. |
| public static int | Returns: UTF-16 offsetThe UTF-16 string buffer source, int UTF-32 offset offset32)Returns the UTF-16 offset that corresponds to a UTF-32 offset. |
| public static int | Returns: UTF-16 offset relative to startThe UTF-16 char array whose substring is to be analysed source, int Offset of the substring to be analysed start, int Offset of the substring to be analysed limit, int UTF-32 offset relative to start offset32)Returns the UTF-16 offset that corresponds to a UTF-32 offset. |
| public static int | Returns: 2 if is in supplementary space, otherwise 1.The input codepoint. char32)Determines how many chars this char32 requires. |
| public static char | Returns: lead surrogate if the getCharCount(ch) is 2;and 0 otherwise (note: 0 is not a valid lead surrogate). The input character. char32)Returns the lead surrogate. |
| public static int | Returns: the code point IF the string is non-null and consists of a single code point. otherwise returns -1.to test s)Utility for getting a code point from a CharSequence that contains exactly one code point. |
| public static char | Returns: the trail surrogate if the getCharCount(ch) is 2;otherwise the character itself The input character. char32)Returns the trail surrogate. |
| public static boolean | Returns: boolean value for whether the string contains more Unicode code points than 'number'.The input string. source, int The number of code points in the string is compared against the 'number'
parameter. number)Check if the string contains more Unicode code points than a certain number. |
| public static boolean | Returns: boolean value for whether the string contains more Unicode code points than 'number'.Array of UTF-16 chars source, int Offset to substring in the source array for analyzing start, int Offset to substring in the source array for analyzing limit, int The number of code points in the string is compared against the 'number'
parameter. number)Check if the sub-range of char array, from argument start to limit, contains more Unicode code points than a certain number. |
| public static boolean | Returns: boolean value for whether the string buffer contains more Unicode code points than 'number'.The input string buffer. source, int The number of code points in the string buffer is compared against the 'number'
parameter. number)Check if the string buffer contains more Unicode code points than a certain number. |
| public static int | Returns: the index of the first occurrence of the codepoint in the argument Unicode string, or -1 if the codepoint does not occur.UTF16 format Unicode string that will be searched source, int Codepoint to search for char32)Returns the index within the argument UTF16 format Unicode string of the first occurrence of the argument codepoint. |
| public static int | Returns: the index of the first occurrence of the codepoint in the argument Unicode string, or -1 if the codepoint does not occur.UTF16 format Unicode string that will be searched source, String UTF16 format Unicode string to search for str)Returns the index within the argument UTF16 format Unicode string of the first occurrence of the argument string str. |
| public static int | Returns: the index of the first occurrence of the codepoint in the argument Unicode string at or after fromIndex, or -1 if the codepoint does not occur.UTF16 format Unicode string that will be searched source, int Codepoint to search for char32, int The index to start the search from. fromIndex)Returns the index within the argument UTF16 format Unicode string of the first occurrence of the argument codepoint. |
| public static int | Returns: the index of the first occurrence of the codepoint in the argument Unicode string, or -1 if the codepoint does not occur.UTF16 format Unicode string that will be searched source, String UTF16 format Unicode string to search for str, int The index to start the search from. fromIndex)Returns the index within the argument UTF16 format Unicode string of the first occurrence of the argument string str. |
| public static StringBuffer | Returns: a reference to targetString buffer to insert to target, int Offset which char32 will be inserted in offset16, int Codepoint to be inserted char32)Inserts char32 codepoint into target at the argument offset16. |
| public static int | Returns: new limit sizeChar array to insert to target, int End index of the char array, limit <= target.length limit, int Offset which char32 will be inserted in offset16, int Codepoint to be inserted char32)Inserts char32 codepoint into target at the argument offset16. |
| public static boolean | Returns: true If the input code point is a lead surrogateThe input character.
(In ICU 2.1-69 the type of this parameter was codePoint)char.)Determines whether the code point is a lead surrogate. |
| public static boolean | Returns: true If the input code point is a surrogate.The input character.
(In ICU 2.1-69 the type of this parameter was codePoint)char.)Determines whether the code point is a surrogate. |
| public static boolean | Returns: true If the input code point is a trail surrogate.The input character.
(In ICU 2.1-69 the type of this parameter was codePoint)char.)Determines whether the code point is a trail surrogate. |
| public static int | Returns: the index of the last occurrence of the codepoint in source, or -1 if the codepoint does not occur.UTF16 format Unicode string that will be searched source, int Codepoint to search for char32)Returns the index within the argument UTF16 format Unicode string of the last occurrence of the argument codepoint. |
| public static int | Returns: the index of the last occurrence of the codepoint in source, or -1 if the codepoint does not occur.UTF16 format Unicode string that will be searched source, String UTF16 format Unicode string to search for str)Returns the index within the argument UTF16 format Unicode string of the last occurrence of the argument string str. |
| public static int | Returns: the index of the last occurrence of the codepoint in source, or -1 if the codepoint does not occur.UTF16 format Unicode string that will be searched source, int Codepoint to search for char32, int the index to start the search from. There is no restriction on the value of
fromIndex. If it is greater than or equal to the length of this string, it has the
same effect as if it were equal to one less than the length of this string: this
entire string may be searched. If it is negative, it has the same effect as if it
were -1: -1 is returned. fromIndex)Returns the index within the argument UTF16 format Unicode string of the last occurrence of the argument codepoint, where the result is less than or equals to fromIndex. |
| public static int | Returns: the index of the last occurrence of the codepoint in source, or -1 if the codepoint does not occur.UTF16 format Unicode string that will be searched source, String UTF16 format Unicode string to search for str, int the index to start the search from. There is no restriction on the value of
fromIndex. If it is greater than or equal to the length of this string, it has the
same effect as if it were equal to one less than the length of this string: this
entire string may be searched. If it is negative, it has the same effect as if it
were -1: -1 is returned. fromIndex)Returns the index within the argument UTF16 format Unicode string of the last occurrence of the argument string str, where the result is less than or equals to fromIndex. |
| public static int | Returns: new shifted offset16string source, int UTF16 position to shift offset16, int number of codepoints to shift shift32)Shifts offset16 by the argument number of codepoints |
| public static int | Returns: new shifted offset16String buffer source, int UTF16 position to shift offset16, int Number of codepoints to shift shift32)Shifts offset16 by the argument number of codepoints |
| public static int | Returns: new shifted offset16 relative to startChar array source, int Position of the subarray to be performed on start, int Position of the subarray to be performed on limit, int UTF16 position to shift relative to start offset16, int Number of codepoints to shift shift32)Shifts offset16 by the argument number of codepoints within a subarray. |
| public static String | Returns: a String representing the code points between offset and countThe code array codePoints, int The start of the text in the code point array offset, int The number of code points count)Cover JDK 1.5 API. |
| public static String | Returns: new String derived from source by replacing every occurrence of oldChar32 with newChar32, unless when no oldChar32 is found in source then source will be returned.UTF16 format Unicode string which the codepoint replacements will be based on. source, int Non-zero old codepoint to be replaced. oldChar32, int The new codepoint to replace oldChar32 newChar32)Returns a new UTF16 format Unicode string resulting from replacing all occurrences of oldChar32 in source with newChar32. |
| public static String | Returns: new String derived from source by replacing every occurrence of oldStr with newStr. When no oldStr is found in source, then source will be returned.UTF16 format Unicode string which the replacements will be based on. source, String Non-zero-length string to be replaced. oldStr, String The new string to replace oldStr newStr)Returns a new UTF16 format Unicode string resulting from replacing all occurrences of oldStr in source with newStr. |
| public static StringBuffer | Returns: a modified source with reversed UTF16 format Unicode string.The source StringBuffer that contains UTF16 format Unicode string to be reversed source)Reverses a UTF16 format Unicode string and replaces source's content with it. |
| public static void | setCharAt(StringBuffer
Stringbuffer target, int UTF16 position to insert into offset16, int Code point char32)Set a code point into a UTF16 position. |
| public static int | Returns: new number of chars in target that represents a stringchar array target, int numbers of valid chars in target, different from target.length. limit counts the
number of chars in target that represents a string, not the size of array target. limit, int UTF16 position to insert into offset16, int code point char32)Set a code point into a UTF16 position in a char array. |
| private static String | Returns: string representation of the code pointcode point ch)Converts argument code point and returns a String object representing the code point's value in UTF16 format. |
| public static String | Returns: string value of char32 in UTF16 formatThe input character. char32)Convenience method corresponding to String.valueOf(char). |
| public static String | |
| public static String | Returns: string value of char32 in UTF16 formatThe input string buffer. source, int The UTF16 index to the codepoint in source offset16)Convenience method corresponding to StringBuffer.valueOf(codepoint at offset16). |
| public static String | Returns: string value of char32 in UTF16 formatThe input char array. source, int Start index of the subarray start, int End index of the subarray limit, int The UTF16 index to the codepoint in source relative to start offset16)Convenience method. |