
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java StringBuffer codePointCount() Method
The Java StringBuffer codePointCount() method counts the number of Unicode code points in the specified text range of this sequence. The text range begins at the specified starting index and extends to the char at second to the last index. Thus the length (in chars) of the text range is ending index beginning index.
If there is no Unicode code point present in the given range of text, the method will not throw any errors and just prints 0.
Note − Unpaired surrogate code points are considered one separate code point each.
Syntax
Following is the syntax for Java StringBuffer codePointCount() method
public int codePointCount(int beginIndex, int endIndex)
Parameters
- beginIndex − This is the index to the first char of the text range.
- endIndex − This is the index after the last char of the text range.
Return Value
This method returns the number of Unicode code points in the specified text range.
Example: Getting length of Text Range
When we consider the input text as the alphabet, the method returns the length of the text range given as arguments.
The following example shows the usage of Java StringBuffer codePointCount() method.
package com.tutorialspoint; public class StringBufferDemo { public static void main(String[] args) { StringBuffer buff = new StringBuffer("TUTORIALS"); System.out.println("buffer = " + buff); // returns the codepoint count from index 1 to 5 int retval = buff.codePointCount(1, 5); System.out.println("Count = " + retval); buff = new StringBuffer("7489042 "); System.out.println("buffer = " + buff); // returns the codepoint count from index 3 to 9 retval = buff.codePointCount(3, 9); System.out.println("Count = " + retval); buff = new StringBuffer("@#$%^&"); System.out.println("buffer = " + buff); // returns the codepoint count from index 2 to 4 retval = buff.codePointCount(2, 4); System.out.println("Count = " + retval); } }
Output
Let us compile and run the above program, this will produce the following result −
buffer = TUTORIALS Count = 4 buffer = 7489042 Count = 6 buffer = @#$%^& Count = 2
Example: Getting length of Text Range having no Valid CodePoints
When we consider the input text as characters that do not have valid codepoints, the method returns zero.
public class StringBufferDemo { public static void main(String[] args) { StringBuffer buff = new StringBuffer("/u1298139"); System.out.println("buffer = " + buff); // returns the codepoint count int retval = buff.codePointCount(0, 0); System.out.println("Count = " + retval); } }
Output
Let us compile and run the above program, this will produce the following result −
buffer = /u1298139 Count = 0
Example: Facing Exception while checking count of CodePoints
However, if the index arguments given exceed or precede the text range, the method throws an IndexOutOfBounds Exception.
public class StringBufferDemo { public static void main(String[] args) { StringBuffer buff = new StringBuffer("djk137"); System.out.println("buffer = " + buff); // returns the codepoint count from index 2 to 4 int retval = buff.codePointCount(-1, 9); System.out.println("Count = " + retval); } }
Exception
If we compile and run the program, an IndexOutOfBounds Exception is thrown instead of printing the output −
buffer = djk137 Exception in thread "main" java.lang.IndexOutOfBoundsException at java.lang.AbstractStringBuilder.codePointCount(AbstractStringBuilder.java:320) at java.lang.StringBuffer.codePointCount(StringBuffer.java:227)at StringBufferDemo.main(StringBufferDemo.java:11)