This article explains a few more Java StringBuffer class methods.
Before reading further, read the previous parts of the series.
- Learn StringBuffer Class in Java: Lecture 1
- Learn StringBuffer Class in Java: Lecture 2
- Learn StringBuffer Class in Java: Lecture 3
- Learn StringBuffer Class in Java: Lecture 4
- Learn StringBuffer Class in Java: Lecture 5
Now let's move forward to other methods of the Java StringBuffer class.
Method: Java StringBuffer codePoint()
This method helps to find the character Unicode code point for the specified index in the given string. It can be categorized into the following three variants:
- CodePointAt(int index).
- CodePointBefore(int index).
- CodePointCount(int beginIndex, int endIndex).

CodePointAt(int index) returns the character Unicode code point at the specified index that refers to char values (Unicode code units) ranging from 0 (zero) to (length – 1).
CodePointBefore(int index) returns the character Unicode code point before the specified index that refers to char values (Unicode code units) ranging from 0 (zero) to total length of the string.
CodePointCount(int beginIndex, int endIndex) returns the number of Unicode code points in the specified text range of the given sequence. The text range lies between the specified beginIndex and endIndex – 1 and thus the length of the text range is endIndex – beginIndex.
Let's see an example to clarify that.
Example
- public class CodePointAtBeforeCountMethods
- {
- public static void main(String args[])
- {
- StringBuffer str1 = new StringBuffer("sixth LECTURE");
- System.out.println("Output for codePointAt():");
- System.out.println();
- int cP1 = str1.codePointAt(2); //return codePoint(small case alphabet x) at index 2
- System.out.println("Code point at index 2 is: " + cP1); //prints the codePoint value
- int cP2 = str1.codePointAt(9); //return codePoint(large case alphabet T) at index 9
- System.out.println("Code point at index 9 is: " + cP2);
- int cP3 = str1.codePointAt(5); //return codePoint at index 5 i.e. of white space char
- System.out.println("Code point at index 5 is: " + cP3);
- System.out.println();
- System.out.println("Output for codePointBefore():");
- System.out.println();
- int cP4 = str1.codePointBefore(4); //return codePoint(small case alphabet t) before index 4
- System.out.println("Code point before index 4 is: " + cP4); //prints the codePoint value
- int cP5 = str1.codePointBefore(7); //return codePoint(large case alphabet L) before index 7
- System.out.println("Code point before index 7 is: " + cP5);
- int cP6 = str1.codePointBefore(6); //return codePoint before index 6 i.e. of white space char
- System.out.println("Code point before index 6 is: " + cP6);
- System.out.println();
- System.out.println("Output for codePointCount():");
- System.out.println();
- int cP7 = str1.codePointCount(0, 3); //return codePoint count from index 0 to 3
- System.out.println("Code point count from index 0 to 3 is: " + cP7); //prints the codePoint value
- int cP8 = str1.codePointCount(6, 13); //return codePoint count from index 6 to 13
- System.out.println("Code point count from index 6 to 13 is: " + cP8);
- int cP9 = str1.codePointCount(2, 7); //return codePoint count from index 2 to 7
- System.out.println("Code point count from index 2 to 7 is: " + cP9);
- }
- }

offsetBycodePoints(int index, int codePointOffset) returns the index within the given sequence that is offset from the given index by codePointOffset code points.




Santhakumar MunuswamyPosted May 23, 2015, 3:21 PM
Thanks for sharing
NitinPosted May 23, 2015, 8:59 AM
nice