StringBuffer setLength(int newLength) Sets the length of the string buffer to newLength. : StringBuffer « Utility Classes « SCJP






public class MainClass{
    public static void main(String[] argv){
        StringBuffer sb = new StringBuffer();
        
        sb.append("this is a test");
        
        sb.setLength(2);
        System.out.println(sb);
    }
}
th








8.8.StringBuffer
8.8.1.Java's StringBuffer and StringBuilder classes represent strings that can be dynamically modified.
8.8.2.StringBuffer provides several versions of the append() method to convert and append other objects and primitive data types to StringBuffer objects.
8.8.3.StringBuffer() Constructs an empty string buffer.
8.8.4.StringBuffer(int capacity) Constructs an empty string buffer with the specified initial capacity.
8.8.5.StringBuffer(String initialString) Constructs a string buffer that initially contains the specified string.
8.8.6.StringBuffer append(String str), StringBuffer append(Object obj)
8.8.7.StringBuffer insert(int offset, String str) Inserts str into the current string buffer at position offset.
8.8.8.StringBuffer reverse() Reverses the characters of the current string buffer.
8.8.9.StringBuffer setCharAt(int offset, char newChar) Replaces the character at position offset with newChar.
8.8.10.StringBuffer setLength(int newLength) Sets the length of the string buffer to newLength.
8.8.11.The StringBuilder class was introduced in 5.0.
8.8.12.String Concatenation the Easy Way