String creates a string of fixed length. While StringBuffer creates a string of flexible length, that can be modified in term of both length and contents. We can insert characters, sub-string in the middle or in the end of the StringBuffer.
Java String Buffer
List of Java String Buffer methods:
S.No.
Method
Syntax
Description
insert()
S1.insert(n,S2)
Insert string S2 at n position in S1.
append()
S1.append('x')
Add any character or string at end of S1.
charAt()
S1.charAt(6)
It will return the 6+1th character from S1.
subString()
S1.subString(6)
Return sub-string, starting from 6th character to end. It will work on both string as well as StringBuffer
subString()
S1.subString(n,m)
Return sub-string starting from n and end at m character.
indexOf()
S1.indexOf('x')
Return the index number of x character in S1.
indexOf()
S1.indexOf('x', n)
Return the index number of x character in S1 starting from nth position.
setLength()
S1.setLength(n)
Sets the length limit of string S1 to n. If n > S1.length () than spaces are added.
reverse()
S.reverse()
Reverse the characters stored in StringBuffer.
replace()
S.replace(5,7,"was")
Replace character at position 5, 6 index and insert "was" in it.