Java: Summary - StringBuffer etc

Constructors
sb = new StringBuffer() Creates empty StringBuffer
sb = new StringBuffer(n) Creates empty StringBuffer with capacity n.
sb = new StringBuffer(s) Creates StringBuffer with value initialized to String s.
Length
i = sb.length() Length of the current contents of sb.
Modifying the content
sb.append(x) Appends x (char, int, String, ...) to end of sb.
sb.insert(offset, x) Inserts x (char, int, String, ...) at position offset.
sb.setCharAt(index, c) Replaces char at index with c
sb.delete(beg, end) Deletes chars at index beg thru end.
sb.reverse() Reverses the contents.
s1 = sb.replace(beg, end, s) Replaces characters beg thru end with s.
Getting parts
c = sb.charAt(i) char at position i in sb.
s = sb.substring(i) substring from index i to the end of sb.
s = sb.substring(i, j) substring from index i to BEFORE index j of sb.
Misc
s = sb.toString() Returns a String for the contents of sb.
Searching -- Note: All "indexOf" methods return -1 if the string/char is not found
i = sb.indexOf(s) index of the first occurrence of String s in s.
i = sb.indexOf(s, i) index of String s at or after position i in sb.
i = sb.lastIndexOf(s) index of last occurrence of s in sb.
i = sb.lastIndexOf(s, i) index of last occurrence of s on or before i in sb.
Comparison (note: use this instead of == and !=)
b = sb.equals(sb2) true if the two StringBuffers contain equal values

java.util.StringTokenizer

StringTokenizer is used to break a string into "words" (tokens).

Constructors
st = new StringTokenizer(s); Creates a StringTokenizer on String s
st = new StringTokenizer(s, delims); As above with delimiter characters in String delims
st = new StringTokenizer(s, delims, true); As above, but also returns delimiters as tokens.
Methods
int st.countTokens() Returns number of tokens. Must scan everything.
boolean st.hasMoreTokens() true if nextToken will return something.
String st.nextToken() Returns next token.
// Typical StringTokenizer loop
import java.util.*;
...
String s = ... // Get a string from input or wherever.
StringTokenizer st = new StringTokenizer(s);
while (st.hasMoreTokens() {
   String token = st.nextToken();
   System.out.println(token); // do something with the token
}

BufferedReader/BufferedWriter Methods

java.io.BufferedReader and java.io.BufferedWriter are used to read/write Strings from/to text files.

Constructors -- see example below
BufferedReader Methods
String br.readLine() Returns next input line without newline char(s) or null if no more input. May throw IOException.
String br.close() Should close when finished reading.
BufferedWriter Methods
void bw.writeLine(s) Writes s to file without newline char(s). May throw IOException.
String bw.close() MUST close when finished writing.
// Example reads from from one file and writes to another.
// Assume inFile and outFile are Strings with path/file names.
try { 
    BufferedReader bufReader = new BufferedReader(new FileReader(inFile)); 
    BufferedWriter bufWriter = new BufferedWriter(new FileWriter(outFile)); 

    String line = null; 
    while ((line=bufReader.readLine())!=null){ 
        bufWriter.write(line); 
        bufWriter.newLine();   // adds newline character(s)
    } 

    bufReader.close(); 
    bufWriter.close(); 

} catch (IOException e) { 
    System.err.println(e); 
    System.exit(1); 
}

Copyright 2004 Fred Swartz Last update: 2004-05-03