Java Lec-16 StringBuffer Class.14a2a2e
Java Lec-16 StringBuffer Class.14a2a2e
Java Lecture- 16
Topic: StringBuffer Class
2. insert()
This method inserts one string into another.
Syntax: StringBuffer insert(int index, T value)
Here the first parameter gives the index at which position the
string will be inserted and T representation of second
parameter is inserted into StringBuffer object.
Here T can be any type like String, int , float, char etc.
Example: StringBuffer sb = new StringBuffer(“HelloWorld”);
sb.insert(5, “Java”);
System.out.println(sb);
Ouput: HelloJavaWorld
3. delete ()
This method removes the characters from the string.
Syntax: StringBuffer delete (int start, int end)
This method is used to delete the string from specified
startIndex and endIndex. Here endindex is exclusive.
Example: StringBuffer sb = new StringBuffer(“HelloJavaWorld”);
sb.delete(5,9);
System.out.println(sb);
Ouput: HelloWorld
4. deleteCharAt()
This method removes the character at the specified index character from
the string.
Syntax: StringBuffer deleteCharAt (int index)
Example: StringBuffer sb = new StringBuffer(“HelloWorld”);
sb.deleteCharAt(4);
System.out.println(sb);
Ouput: HellWorld
5. replace ()
This method replaces the specified string from specified start index to the
end index.
Syntax: StringBuffer replace (int start, int end, String newString)
Example: StringBuffer sb = new StringBuffer(“HelloJavaWorld”);
sb.replace(5,9,”InfoPlanet”);
System.out.println(sb);
Ouput: HellInfoPlanetWorld
6. reverse()
This method reverses the characters within a StringBuffer object.
Syntax: StringBuffer reverse ()
Example: StringBuffer sb = new StringBuffer(“HelloWorld”);
sb.reverse();
System.out.println(sb);
Ouput: dlroWolleH