Skip to content

Instantly share code, notes, and snippets.

@chankok
Last active November 25, 2016 04:33
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/104f1994843f140a82a7179bcef71645 to your computer and use it in GitHub Desktop.
Save chankok/104f1994843f140a82a7179bcef71645 to your computer and use it in GitHub Desktop.
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