Created
September 27, 2016 07:35
-
-
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/
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 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