Top Description Inners Fields Constructors Methods
com.ibm.icu.text

public final Class UTF16

extends Object
Class Inheritance
Imports
com.ibm.icu.impl.Utility

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().

Examples:

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:
Author
Mark Davis, with help from Markus Scherer
ICU Status
Stable since ICU 2.1.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
UTF16.StringComparator

UTF16 string comparator class.

Field Summary

Modifier and TypeField 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
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 bounds().

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
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

Constructor Summary

AccessConstructor and Description
private
UTF16()

Prevent instance from being created.

Method Summary

Modifier and TypeMethod and Description
private static int
_charAt(String source, int offset16, char single)

private static int
_charAt(CharSequence source, int offset16, char single)

public static StringBuffer

Returns:

the updated StringBuffer
append
(StringBuffer
The 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.
append
(char[]
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 StringBuffer
appendCodePoint
(StringBuffer
The buffer to append to
target
,
int
The code point to append
cp
)

Cover JDK 1.5 APIs.

public static int

Returns:

  • SINGLE_CHAR_BOUNDARY : a single char; the bounds are [offset16, offset16+1]
  • LEAD_SURROGATE_BOUNDARY : a surrogate pair starting at offset16; the bounds are [offset16, offset16 + 2]
  • TRAIL_SURROGATE_BOUNDARY : a surrogate pair starting at offset16 - 1; the bounds are [offset16 - 1, offset16 + 1]
For bit-twiddlers, the return values for these are chosen so that the boundaries can be gotten by: [offset16 - (value >> 2), offset16 + (value & 3)].
bounds
(String
Text to analyse
source
,
int
UTF-16 offset
offset16
)

Returns the type of the boundaries around the char at offset16.

public static int

Returns:

  • SINGLE_CHAR_BOUNDARY : a single char; the bounds are [offset16, offset16 + 1]
  • LEAD_SURROGATE_BOUNDARY : a surrogate pair starting at offset16; the bounds are [offset16, offset16 + 2]
  • TRAIL_SURROGATE_BOUNDARY : a surrogate pair starting at offset16 - 1; the bounds are [offset16 - 1, offset16 + 1]
For bit-twiddlers, the return values for these are chosen so that the boundaries can be gotten by: [offset16 - (value >> 2), offset16 + (value & 3)].
bounds
(StringBuffer
String buffer to analyse
source
,
int
UTF16 offset
offset16
)

Returns the type of the boundaries around the char at offset16.

public static int

Returns:

  • SINGLE_CHAR_BOUNDARY : a single char; the bounds are
  • LEAD_SURROGATE_BOUNDARY : a surrogate pair starting at offset16; the bounds are [offset16, offset16 + 2]
  • TRAIL_SURROGATE_BOUNDARY : a surrogate pair starting at offset16 - 1; the bounds are [offset16 - 1, offset16 + 1]
For bit-twiddlers, the boundary values for these are chosen so that the boundaries can be gotten by: [offset16 - (boundvalue >> 2), offset16 + (boundvalue & 3)].
bounds
(char[]
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

Returns:

UTF-32 value for the UTF-32 value that contains the char at offset16. The boundaries of that codepoint are the same as in bounds32().
charAt
(String
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 in bounds32().
charAt
(CharSequence
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 in bounds32().
charAt
(StringBuffer
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 in bounds32().
charAt
(char[]
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 in bounds32().
charAt
(Replaceable
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.
compareCodePoint
(int
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 string
countCodePoint
(String
UTF16 string
source
)

Number of codepoints in a UTF16 String

public static int

Returns:

number of codepoint in string
countCodePoint
(StringBuffer
UTF16 string buffer
source
)

Number of codepoints in a UTF16 String buffer

public static int

Returns:

number of codepoint in the substring
countCodePoint
(char[]
UTF16 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 target
delete
(StringBuffer
String 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 size
delete
(char[]
String 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 offset
findCodePointOffset
(String
Text 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 offset
findCodePointOffset
(StringBuffer
Text 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 start
findCodePointOffset
(char[]
Text 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 offset
findOffsetFromCodePoint
(String
The 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 offset
findOffsetFromCodePoint
(StringBuffer
The 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 start
findOffsetFromCodePoint
(char[]
The 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.
getCharCount
(int
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).
getLeadSurrogate
(int
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.
getSingleCodePoint
(CharSequence
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
getTrailSurrogate
(int
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'.
hasMoreCodePointsThan
(String
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'.
hasMoreCodePointsThan
(char[]
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'.
hasMoreCodePointsThan
(StringBuffer
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.
indexOf
(String
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.
indexOf
(String
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.
indexOf
(String
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.
indexOf
(String
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 target
insert
(StringBuffer
String 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 size
insert
(char[]
Char 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 surrogate
isLeadSurrogate
(int
The input character. (In ICU 2.1-69 the type of this parameter was char.)
codePoint
)

Determines whether the code point is a lead surrogate.

public static boolean

Returns:

true If the input code point is a surrogate.
isSurrogate
(int
The input character. (In ICU 2.1-69 the type of this parameter was char.)
codePoint
)

Determines whether the code point is a surrogate.

public static boolean

Returns:

true If the input code point is a trail surrogate.
isTrailSurrogate
(int
The input character. (In ICU 2.1-69 the type of this parameter was char.)
codePoint
)

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.
lastIndexOf
(String
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.
lastIndexOf
(String
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.
lastIndexOf
(String
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.
lastIndexOf
(String
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 offset16
moveCodePointOffset
(String
string
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
moveCodePointOffset
(StringBuffer
String 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 start
moveCodePointOffset
(char[]
Char 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 count
newString
(int[]
The 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.
replace
(String
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.
replace
(String
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.
reverse
(StringBuffer
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 string
setCharAt
(char[]
char 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 point
toString
(int
code 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 format
valueOf
(int
The input character.
char32
)

Convenience method corresponding to String.valueOf(char).

public static String

Returns:

string value of char32 in UTF16 format
valueOf
(String
The input string.
source
,
int
The UTF16 index to the codepoint in source
offset16
)

Convenience method corresponding to String.valueOf(codepoint at offset16).

public static String

Returns:

string value of char32 in UTF16 format
valueOf
(StringBuffer
The 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 format
valueOf
(char[]
The 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.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait