Learn StringBuffer() Class in Java: Lecture 6

This article explains a few more Java StringBuffer class methods.
Before reading further, read the previous parts of the series.
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
  1. public class CodePointAtBeforeCountMethods  
  2. {  
  3.     public static void main(String args[])   
  4.     {  
  5.         StringBuffer str1 = new StringBuffer("sixth LECTURE");  
  6.         System.out.println("Output for codePointAt():");  
  7.         System.out.println();  
  8.         int cP1 = str1.codePointAt(2); //return codePoint(small case alphabet x) at index 2  
  9.         System.out.println("Code point at index 2 is: " + cP1); //prints the codePoint value  
  10.         int cP2 = str1.codePointAt(9); //return codePoint(large case alphabet T) at index 9  
  11.         System.out.println("Code point at index 9 is: " + cP2);  
  12.         int cP3 = str1.codePointAt(5); //return codePoint at index 5 i.e. of white space char  
  13.         System.out.println("Code point at index 5 is: " + cP3);  
  14.         System.out.println();  
  15.         System.out.println("Output for codePointBefore():");  
  16.         System.out.println();  
  17.         int cP4 = str1.codePointBefore(4); //return codePoint(small case alphabet t) before index 4  
  18.         System.out.println("Code point before index 4 is: " + cP4); //prints the codePoint value  
  19.         int cP5 = str1.codePointBefore(7); //return codePoint(large case alphabet L) before index 7  
  20.         System.out.println("Code point before index 7 is: " + cP5);  
  21.         int cP6 = str1.codePointBefore(6); //return codePoint before index 6 i.e. of white space char  
  22.         System.out.println("Code point before index 6 is: " + cP6);  
  23.         System.out.println();  
  24.         System.out.println("Output for codePointCount():");  
  25.         System.out.println();  
  26.         int cP7 = str1.codePointCount(03); //return codePoint count from index 0 to 3  
  27.         System.out.println("Code point count from index 0 to 3 is: " + cP7); //prints the codePoint value  
  28.         int cP8 = str1.codePointCount(613); //return codePoint count from index 6 to 13  
  29.         System.out.println("Code point count from index 6 to 13 is: " + cP8);  
  30.         int cP9 = str1.codePointCount(27); //return codePoint count from index 2 to 7  
  31.         System.out.println("Code point count from index 2 to 7 is: " + cP9);  
  32.     }  
  33. }
Output
 
One additional method related to codePoint() should also be discussed here, offsetByCodePoints().
offsetBycodePoints(int index, int codePointOffset) returns the index within the given sequence that is offset from the given index by codePointOffset code points.
 
Example
  1. public class OffsetByCodePointsMethod   
  2. {  
  3.     public static void main(String args[])   
  4.     {  
  5.         StringBuffer str1 = new StringBuffer("Discussions");  
  6.         System.out.println("Output for offsetBycodePoint():");  
  7.         System.out.println();  
  8.         int cP1 = str1.offsetByCodePoints(28); //return index within the given sequence  
  9.         System.out.println("Index value within given seq is: " + cP1); //prints the index value  
  10.         int cP2 = str1.offsetByCodePoints(44);  
  11.         System.out.println("Index value within given seq is: " + cP2);  
  12.         int cP3 = str1.offsetByCodePoints(07);  
  13.         System.out.println("Index value within given seq is: " + cP3);  
  14.     }  
  15. }
Output
 
 
In the preceding output, actually what is happening is that after counting the given index value in the string or CharSeq, it continues to count the CharSeq until the given value of codePointOffset and it returns the total count value of CharSeq.
 
Now the next method is delete().
 
Java StringBuffer delete() method removes or deletes the characters from the specified index position of a substring of the given CharSequence.
 
There are the following two types of method.
  • StringBuffer delete(int start, int end).
  • StringBuffer deleteCharAt(int index).
 
delete(int start, int end) removes or deletes the characters from a substring of the given sequence. The chars of the substring lie between the index values startIndex and endIndex-1 or to end of the given sequence if no such characters exist. No changes are made if the index values of the start and end are equal. It throws StringIndexOutOfBoundsException() if the value of start is negative or greater than the length of the given sequence or greater than the end value.
 
deleteCharAt(int index) removes or deletes the characters at the specified index position in the given sequence that is shortened by one character. It also throws StringIndexOutOfBoundsException() if the index value is negative or greater than or equal to the length of the given sequence.
 
Let's see the example for both.
 
Example
  1. public class DeleteAndDelCharAtMethods   
  2. {  
  3.     public static void main(String args[])   
  4.     {  
  5.         StringBuffer str1 = new StringBuffer("God will bless you");  
  6.         System.out.println("Output for delete():");  
  7.         System.out.println();  
  8.         System.out.println("Buffer is: " + str1);  
  9.         str1.delete(38); //delete chars from index 3 to 8  
  10.         System.out.println("String after deletion is: " + str1); //prints the string  
  11.         str1 = new StringBuffer("God will bless you");  
  12.         str1.delete(413); //delete chars from index 4 to 13  
  13.         System.out.println("String after deletion is: " + str1);  
  14.         System.out.println();  
  15.         StringBuffer str2 = new StringBuffer("123-4500006-789");  
  16.         System.out.println("Buffer is: " + str2);  
  17.         str2.delete(610); //delete chars from index 6 to 10  
  18.         System.out.println("String after deletion is: " + str2); //prints the string  
  19.         str2 = new StringBuffer("123-4500006-789");  
  20.         str2.delete(411); //delete chars from index 4 to 11  
  21.         System.out.println("String after deletion is: " + str2);  
  22.         System.out.println();  
  23.         System.out.println("Output for deleteCharAt():");  
  24.         System.out.println();  
  25.         StringBuffer str3 = new StringBuffer("Java lang");  
  26.         System.out.println("Buffer is: " + str3);  
  27.         str3.deleteCharAt(2); //delete char at index 2  
  28.         System.out.println("String after deletion is: " + str3); //prints the string  
  29.         str3 = new StringBuffer("Java lang");  
  30.         str3.deleteCharAt(5).deleteCharAt(5); //delete char at index 5 & 6  
  31.         System.out.println("String after deletion is: " + str3);  
  32.         System.out.println();  
  33.         StringBuffer str4 = new StringBuffer("3$450006$7");  
  34.         System.out.println("Buffer is: " + str4);  
  35.         str4.deleteCharAt(1); //delete char at index 1  
  36.         System.out.println("String after deletion is: " + str4); //prints the string  
  37.         str4 = new StringBuffer("3$450006$7");  
  38.         str4.deleteCharAt(4).deleteCharAt(4).deleteCharAt(4); //delete chars at  
  39.         // index 4,5,6  
  40.         System.out.println("String after deletion is: " + str4);  
  41.     }  
  42. }
Output
 
In the preceding output, we can observe that for deleteCharAt(), when we apply the method in continuity, the next character (char positioned after the deleting char) shifts its index position one less after each deletion and comes at the index position of the previous char. That is why the index value given in the example is the same for all continuous methods.
 
For next lecture click the below link.
 
 
Thank you, keep learning and sharing.


Similar Articles