StringBuffer Replace In Java
Chapter:
String Handling
Last Updated:
13-09-2016 17:32:23 UTC
Program:
/* ............... START ............... */
public class JavaStringBufferReplace {
public static void main(String[] args) {
// Create the StringBuffer object
StringBuffer stringBuffer = new StringBuffer("Hello Java");
System.out.println("Original Text : " + stringBuffer);
stringBuffer.replace(0, 5, "Hi");
System.out.println("Replaced Text : " + stringBuffer);
}
}
/* ............... END ............... */
Output
Original Text : Hello Java
Replaced Text : Hi Java
Notes:
-
Replace method replaces the characters in a substring of this StringBuffer with characters in the specified String.
- Syntax : public StringBuffer replace(int start, int end, String str).
- start -- The beginning index.
- end -- The ending index, exclusive.
- str -- String that will replace previous contents.
Tags
StringBuffer Replace, Java, String