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.
Syntax:
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:
Example 1:
// Java program to demonstrate // the codePointCount() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("WelcomeGeeks"); // print string System.out.println("String = " + str.toString()); // returns the codepoint count from index 2 to 8 int codepoints = str.codePointCount(2, 8); System.out.println("No of Unicode code points = " + codepoints); } } |
String = WelcomeGeeks No of Unicode code points = 6
Example 2:
// Java program to demonstrate // the codePointCount() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("GeeksForGeeks"); // print string System.out.println("String = " + str.toString()); // returns the codepoint count // from index 3 to 7 int codepoints = str.codePointCount(3, 7); System.out.println("No of Unicode code points" + " between index 3 and 7 = " + codepoints); } } |
String = GeeksForGeeks No of Unicode code points between index 3 and 7 = 4
Example 3: To demonstrate IndexOutOfBoundsException
// Java program to demonstrate // exception thrown by the codePointCount() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("GeeksForGeeks"); try { // make beginIndex greater than endIndex int codepoints = str.codePointCount(7, 4); } catch (Exception e) { System.out.println("Exception: " + e); } } } |
Exception: java.lang.IndexOutOfBoundsException
Reference:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#codePointCount(int, int)
Recommended Posts:
- StringBuffer codePointCount() method in Java with Examples
- StringBuilder Class in Java with Examples
- StringBuilder deleteCharAt() in Java with Examples
- StringBuilder charAt() in Java with Examples
- StringBuilder setCharAt() in Java with Examples
- StringBuilder capacity() in Java with Examples
- StringBuilder reverse() in Java with Examples
- StringBuilder replace() in Java with Examples
- StringBuilder getChars() in Java with Examples
- StringBuilder codePointAt() in Java with Examples
- StringBuilder subSequence() in Java with Examples
- StringBuilder setLength() in Java with Examples
- StringBuilder ensureCapacity() in Java with Examples
- StringBuilder delete() in Java with Examples
- StringBuilder length() in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



