Topics Index

StringBufferDemo4 - Java Example Program

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

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


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.

  • 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".

© meritcampus 2019

All Rights Reserved.

Open In App