Skip to content

Instantly share code, notes, and snippets.

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