Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@chankok
Created September 27, 2016 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chankok/5ea6562755b9571e669c8ec7e858d938 to your computer and use it in GitHub Desktop.
Save chankok/5ea6562755b9571e669c8ec7e858d938 to your computer and use it in GitHub Desktop.
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