Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Java Example: Modify Strings in StringBuffer http://www.chankok.com/java-modify-strings-in-stringbuffer/
public class StringBufferExample {
public static void main(String[] args) {
StringBuffer strBuf = new StringBuffer();
// Append strings
strBuf.append("Welcome").append(' ').append("to").append(' ').append("Chankok.com");
System.out.println("Append strings : " + strBuf.toString());
// Insert Strings
strBuf.insert(11, "Java world and ");
System.out.println("Insert strings : " + strBuf.toString());
// Replace character and strings
strBuf.setCharAt(16, 'W');
System.out.println("Replace character: " + strBuf.toString());
strBuf.replace(16, 21, "blog");
System.out.println("Replace strings : " + strBuf.toString());
// Delete character and strings
strBuf.deleteCharAt(11);
System.out.println("Delete character : " + strBuf.toString());
strBuf.delete(11, 24);
System.out.println("Delete strings : " + strBuf.toString());
strBuf.setLength(0);
System.out.println("Empty strings : " + strBuf.toString());
// Reverse strings
strBuf.append("Welcome to Chankok.com");
strBuf.reverse();
System.out.println("Reverse strings : " + strBuf.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment