Menu
Topics Index
...
`

StringBuilderDemo - Java Example Program


StringBuilderDemo
class StringBuilderDemo
{
    public static void main(String[] args) {
    
        StringBuilder sb = new StringBuilder("Merit"); // LINE A
        System.out.println("StringBuilder = " + sb);
    
        // appends the string argument to the StringBuilder
        sb.append(" Campus"); // LINE B
        // print the StringBuilder after appending
        System.out.println("After append = " + sb);
    
        // insert string value at offset 0
        sb.insert(0, "Java "); // LINE C
    
        // prints StringBuilder after insertion
        System.out.print("After insertion = " + sb);
    
    }

}
OUTPUT

StringBuilder = Merit
After append = Merit Campus
After insertion = Java Merit Campus

DESCRIPTION

First "Merit" is taken into StringBuilder. Using append method, " Campus" is appended to the StringBuilder. Finally using insert method "Java " is insert at offset 0.

THINGS TO TRY
  • Try using other inputs for StringBuilder at LINE A.
  • Try using other inputs in append at LINE B.
  • Try using other inputs in insert at LINE C.
  • Try using other methods by referring to the links given above.

© meritcampus 2019

All Rights Reserved.

Open In App