String Handling (StringBuilder Class)
String Handling (StringBuilder Class)
◦ StringBuilder( )
◦ StringBuilder(int cap)
◦ StringBuilder(String str)
If the newLength argument is less than the current length, the new
length of character sequence will change to newLength. On the
other hand, if the newLength argument is greater than the current
length then the null character(s) '\u0000' are appended so that length
becomes the newLength argument.
sb.setLength(6);
System.out.println("set new length:
"+sb.length());
class insertDemo {
public static void main(String args[]) {
StringBuilder sb = new StringBuilder("I Java!");
sb.insert(2, "like ");
System.out.println(sb);
}
}
// I like Java!
reverse( )
Used to reverse the characters within a StringBuilder object.
This method returns the reversed object on which it was called.
StringBuilder reverse()
Example:
class ReverseDemo {
public static void main(String args[]) {
StringBuilder s = new StringBuilder(“ Apple");
System.out.println(s);
s.reverse();
System.out.println(s);
}
}
delete( ) and deleteCharAt( )
Used to delete characters within a StringBuilder.
The first form returns the substring that starts at startIndex and
runs to the end of the invoking StringBuilder object.