Last active
November 25, 2016 04:33
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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