StringBuffer capacity()
The current length of a StringBuffer can be found via the length( ) method.
The total allocated capacity can be found through the capacity( ) method.
int capacity()
- Returns the current capacity.
int length()
- Returns the length (character count).
public class Main {
public static void main(String[] argv) {
StringBuffer sb = new StringBuffer();
sb.append("java2s.com");
System.out.println(sb.length());
System.out.println(sb.capacity());
}
}
The output:
10
16
public class Main {
public static void main(String[] argv) {
StringBuffer sb = new StringBuffer();
sb.append("java2s.com");
System.out.println(sb.length());
System.out.println(sb.capacity());
sb.trimToSize();
System.out.println(sb.length());
System.out.println(sb.capacity());
}
}
The output:
10
16
10
10
Home
Java Book
Essential Classes
Java Book
Essential Classes
StringBuffer:
- Create StringBuffer object
- append
- StringBuffer capacity()
- charAt(int index): get the char at specified index
- delete(int start, int end) and deleteCharAt(int index)
- ensureCapacity( )
- getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
- indexOf(String str)
- lastIndexOf(String str)
- StringBuffer length()
- Insert(): add data in the middle of a StringBuffer
- replace():replace a StringBuffer
- StringBuffer reverse()
- setCharAt(int index, char ch)
- setLength
- substring
- toString():Convert StringBuffer to String