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.
- 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);
- System.out.println("Code point at index 2 is: " + cP1);
- int cP2 = str1.codePointAt(9);
- System.out.println("Code point at index 9 is: " + cP2);
- int cP3 = str1.codePointAt(5);
- 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);
- System.out.println("Code point before index 4 is: " + cP4);
- int cP5 = str1.codePointBefore(7);
- System.out.println("Code point before index 7 is: " + cP5);
- int cP6 = str1.codePointBefore(6);
- 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);
- System.out.println("Code point count from index 0 to 3 is: " + cP7);
- int cP8 = str1.codePointCount(6, 13);
- System.out.println("Code point count from index 6 to 13 is: " + cP8);
- int cP9 = str1.codePointCount(2, 7);
- System.out.println("Code point count from index 2 to 7 is: " + cP9);
- }
- }
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
- public class OffsetByCodePointsMethod
- {
- public static void main(String args[])
- {
- StringBuffer str1 = new StringBuffer("Discussions");
- System.out.println("Output for offsetBycodePoint():");
- System.out.println();
- int cP1 = str1.offsetByCodePoints(2, 8);
- System.out.println("Index value within given seq is: " + cP1);
- int cP2 = str1.offsetByCodePoints(4, 4);
- System.out.println("Index value within given seq is: " + cP2);
- int cP3 = str1.offsetByCodePoints(0, 7);
- System.out.println("Index value within given seq is: " + cP3);
- }
- }
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.
- public class DeleteAndDelCharAtMethods
- {
- public static void main(String args[])
- {
- StringBuffer str1 = new StringBuffer("God will bless you");
- System.out.println("Output for delete():");
- System.out.println();
- System.out.println("Buffer is: " + str1);
- str1.delete(3, 8);
- System.out.println("String after deletion is: " + str1);
- str1 = new StringBuffer("God will bless you");
- str1.delete(4, 13);
- System.out.println("String after deletion is: " + str1);
- System.out.println();
- StringBuffer str2 = new StringBuffer("123-4500006-789");
- System.out.println("Buffer is: " + str2);
- str2.delete(6, 10);
- System.out.println("String after deletion is: " + str2);
- str2 = new StringBuffer("123-4500006-789");
- str2.delete(4, 11);
- System.out.println("String after deletion is: " + str2);
- System.out.println();
- System.out.println("Output for deleteCharAt():");
- System.out.println();
- StringBuffer str3 = new StringBuffer("Java lang");
- System.out.println("Buffer is: " + str3);
- str3.deleteCharAt(2);
- System.out.println("String after deletion is: " + str3);
- str3 = new StringBuffer("Java lang");
- str3.deleteCharAt(5).deleteCharAt(5);
- System.out.println("String after deletion is: " + str3);
- System.out.println();
- StringBuffer str4 = new StringBuffer("3$450006$7");
- System.out.println("Buffer is: " + str4);
- str4.deleteCharAt(1);
- System.out.println("String after deletion is: " + str4);
- str4 = new StringBuffer("3$450006$7");
- str4.deleteCharAt(4).deleteCharAt(4).deleteCharAt(4);
-
- System.out.println("String after deletion is: " + str4);
- }
- }
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.