StringBuilder Constructor
StringBuilder()
- Creates a string builder and an initial capacity of 16 characters.
StringBuilder(CharSequence seq)
- Creates a string builder from the specified CharSequence.
StringBuilder(int capacity)
- Creates a string builder with no characters in it and an initial capacity specified by the capacity argument.
StringBuilder(String str)
- Creates a string builder initialized to the contents of the specified string.
public class Main {
public static void main(String[] argv) {
StringBuilder sb = new StringBuilder(20);
sb.append("java2s.com");
System.out.println(sb.capacity());
}
}
The output:
20
Using new StringBuilder(String str)
public class Main {
public static void main(String[] args) {
StringBuilder lipsum = new StringBuilder("Lorem ipsum dolor sit amet.");
System.out.println("lipsum = " + lipsum.toString());
lipsum.delete(0, 28);
System.out.println("lipsum = " + lipsum.toString());
lipsum.deleteCharAt(lipsum.length() - 1);
System.out.println("lipsum = " + lipsum.toString());
}
}
Using new StringBuilder(int capacity)
public class Main {
public static void main(String[] a) {
StringBuilder builder1 = new StringBuilder();
StringBuilder builder2 = new StringBuilder(0);
StringBuilder builder3 = new StringBuilder(100);
System.out.println(builder1.capacity());
System.out.println(builder2.capacity());
System.out.println(builder3.capacity());
}
}
Output:
16
0
100