StringBuilder codePointCount() in Java with Examples
The codePointBefore() method of StringBuilder class returns the number of Unicode code points in the specified text range in String contained by StringBuilder. This method takes two indexes as a parameter- first beginIndex which represents index of the first character of the text range and endIndex which represents index after the last character of the text range. The indexes refers to char values (Unicode code units) and the value of index must be lie between 0 to length-1. The text range begins at the beginIndex and extends to the char at index endIndex – 1. Thus the length (in chars) of the text range is endIndex-beginIndex.
public int codePointCount(int beginIndex, int endIndex)
Parameters: This method accepts two parameters
- beginIndex: index of the first character of the text range.
- endIndex: index after the last character of the text range.
Return Value: This method returns the number of Unicode code points in the specified text range.
Exception: This method throws IndexOutOfBoundsException if:
- the beginIndex is less than zero,
- or endIndex is larger than the length of String,
- or beginIndex is larger than endIndex.
Below programs demonstrate the codePointCount() method of StringBuilder Class:
String = WelcomeGeeks No of Unicode code points = 6
String = GeeksForGeeks No of Unicode code points between index 3 and 7 = 4
Example 3: To demonstrate IndexOutOfBoundsException