Menu
Topics Index
...
`


Strings > StringBuffer >
Siva Nookala - 20 Feb 2017
Additional StringBuffer methods are as follows:

StringBuffer Methods:
MethodDescription
int codePointAt(int index)Returns the character (Unicode code point) at the specified index.
int codePointBefore(int index)Returns the character (Unicode code point) before the specified index.
int codePointCount(int beginIndex, int endIndex)Returns the number of Unicode code points in the specified text range of this sequence.
int indexOf(String str)Returns the index within this string of the first occurrence of the specified substring.
int indexOf(String str, int fromIndex)Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
int lastIndexOf(String str)Returns the index within this string of the rightmost occurrence of the specified substring.
int lastIndexOf(String str, int fromIndex)Returns the index within this string of the last occurrence of the specified substring.
int offsetByCodePoints(int index, int codePointOffset)Returns the index within this sequence that is offset from the given index by codePointOffset code points.
StringBuffer replace(int start, int end, String str)Replaces the characters in a substring of this sequence with characters in the specified String.
StringBuffer reverse()Causes this character sequence to be replaced by the reverse of the sequence.
CharSequence subSequence(int start, int end)Returns a new character sequence that is a subsequence of this sequence.
String toString()Returns a string representing the data in this sequence.
void trimToSize()Attempts to reduce storage used for the character sequence.

StringBufferDemo4
class StringBufferDemo4
{
    public static void main(String args[])
    {
    
        StringBuffer sb=new StringBuffer("Java Merit Campus");
    
        // returns the codepoint at index 5
        System.out.println("Unicode = " + sb.codePointAt(5)); // LINE A
    
        // returns the index of the specified substring
        System.out.println("Substring Index = " + sb.indexOf("Camp")); // LINE B
    
        // returns the index as -1 if not found
        System.out.println("Substring Index = " + sb.indexOf("camp")); //LINE C
    
        //reverse characters of the buffer and prints it
        System.out.println("Reverse = " + sb.reverse()); //LINE D
    
    }
}
OUTPUT

Unicode = 77
Substring Index = 11
Substring Index = -1
Reverse = supmaC tireM avaJ

DESCRIPTION

Firstly, the Unicode of the character in the StringBuffer at index 5 is printed. In LINE B and LINE C the index of the string in the StringBuffer is returned. Finally, the reverse of the StringBuffer is printed.

THINGS TO TRY
  • Try using the method codePointBefore instead of codePointAt in LINE A.
  • Try using the method lastIndexOf instead of indexOf in LINE B and LINE C.
  • Try using the other methods of StringBuffer as listed above for other inputs like "Don't just learn Java, do Java", "Your Online Java School".

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.