JAVATM MASTER

String Replace Method

Back to Table of Contents

Prior to javaTM 1.5, to replace one String with another String within a String, an elaborate method had to be accomplished, which I show at the bottom of this section. Now, since javaTM 1.5 and later, you can perform a String replace like this:

String myString = "She sells sea shells.";
myString = myString.replace("ell", "ow"); 

myString will now be: "She sows sea shows."

Note how you assign the result of the replace() back to the same String variable. This is needed as String Objects are immutable. The above code creates a new String object with the replaced data and garbage collects the previous String Object as the variable, myString, no longer refers to it.

Note, Strings are immutable; when you change the value of a String it becomes a new String and any other String references to that String still have the original value After this method completes, the variable Text will still have the original value while the String returned will be the result of the String replacement in the Text String.

    /**
     * Used to replace one String segment with
     * another String segment inside a String.
     * Similar to the replace method in String but instead of using
     * char it uses String for replacing old with new. Static method.
     * @param Text The String from which is produced the new String 
     * with which replacement has occurred.
     * @param Old The old String that is replaced by the new one
     * in The Text String.
     * @param New The new String to replace the old String 
     * in the Text String.
     * @return The new String with replacement having occurred.
     */
	public static String replace(String Text, String Old, String New){
		if (Old.length() == 0) return Text;
		StringBuffer buf = new StringBuffer();
		int i=0, j=0;
		while((i = Text.indexOf(Old, j)) > -1){
			buf.append(Text.substring(j,i) + New);
			j = i + Old.length();
		}
		if (j < Text.length())
			buf.append(Text.substring(j));
		return buf.toString();
	}
Usage: String Text = "This is a demo";
String Text2 = replace(Text, "is", "ose");
After this Text2 will have the value: "Those ose a demo",
while Text will still be of value: "This is a demo".

You see, the makers of JavaTM made the String class to behave similar to primitive types. Strings are final, you cannot extend them. Strings can use the + and += operators and any Object that is to the right of a + or += operator will be converted to a String by calling its toString() method.

Because Strings become new Strings whenever + or += is operated upon them, when doing a lot of such operations it is better to use StringBuffer which behaves like other objects, where the append method adds data to it without instantiating a new StringBuffer.

To replace one character with another, it is more efficient to use the String.replace method [String replace(char old, char new)].

*OracleTM and JavaTM are registered trademarks of Oracle and or its affiliates. Other names may be trademarks of their respective owners.*