|
![]() |
/* Assume StringBuffer sb = new StringBuffer(); StringBuffer sb2; int i, offset, len; char c; String s; char chararr[]; Constructors sb = new StringBuffer(); Creates new, empty, StringBuffer sb = new StringBuffer(n); Creates new StringBuffer of size n sb = new StringBuffer(s); Creates new StringBuffer with initial value s Building StringBuffer sb2 = sb.append(x) appends x (any primitive or object type) to end of sb. sb2 = sb.append(chararr, offset, len) appends len chars from chararr starting at index offset. sb2 = sb.insert(offset, x) inserts x (char, int, String, ...) at position offset. sb.setCharAt(index, c) replaces char at index with c Deleting from StringBuffer sb2 = sb.delete(beg, end) deletes chars at index beg thru end. sb.setLength(n) Sets the length of the content to n by either truncating current content or extending it with the null character ('\u0000'). Use sb.setLength(0); to clear a string buffer. Extracting from StringBuffer c = sb.charAt(i) char at position i. s = sb.substring(start) substring from position start to end of string. s = sb.substring(start, end) substring from position start to the char before end. s = sb.toString() Returns String. Searching i = sb.indexOf(s) Returns position of first (leftmost) occurrence of s in sb. i = sb.lastIndexOf(s) Returns position of last (rightmost) occurrence of s in sb. Misc i = sb.length() length of the string s. sb2 = sb.reverse() */ |