How to replace characters and substring in Java? String.replace(), replaceAll() and replaceFirst() Example (original) (raw)

One of the common programming tasks is to replace characters or substring from a String object in Java. For example, you have a String "internet" and you want to replace the letter "i" with the letter "b", how do you that? Well, the String class in Java provides several methods to replace characters, CharSequence, and substring from a String in Java. You can call replace method on the String, where you want to replace characters and it will return a result where characters are replaced. What is the most important point to remember is that the result object would be a new String object?

Since String is immutable in Java, every time you perform an operation on String either replacement or removing white space from String, it generates a new String object. The good thing about these methods is that they support regular expression, which means you can specify a pattern and all the characters which match the pattern will be replaced.

There are four overloaded method to replace String in Java :

Out of these, 2nd one which takes a CharSequence is added on Java 1.5. CharSequence is actually a super interface for String, StringBuffer and StringBuilder in Java, which means you can pass any of String, StringBuffer or StringBuilder Object as an argument to this replace method.

The replaceFirst() and replaceAll() are very powerful and accepts regular expression. replaceFirst() only replace first match, while replaceAll replaces all matches with replacement String provided.

How to replace character or substring in Java? replace() example

In this Java tutorial, we will see How to replace characters and substring from String in Java. First example in this program replaces a character, i.e. it replaces "J" with "K", which creates "Kava" from "Java". Second String replace example, replaces words from String, it replaces Scala with Java.

Third and fourth example shows how to use regular expression to replace String in Java. Both examples uses \\s to match spaces or any white space character and replace with #.

Third one uses replaceFirst() which only replace first match, while fourth one uses replaceAll() which replaces all matches.

package test;

/**

}

Output: Replacing character in String Original String before replace : Java Replaced String : Kava String before replace : Scala is good programming language String after replace : Java is good programming language Replacing first match of regex using replaceFirst() Original String before replacement : Scala is good programming language Final String : Scala#is good programming language Replacing all occurrence of substring which match regex ReplaceAll Example : Scala#is#good#programming#language

That's it on How to replace String in Java. As explained java.lang.String class provides multiple overloaded methods, which can replace a single character or substring in Java. replaceAll() in particular is very powerful and can replace all occurrences of any matching character or regular expression in Java.

It also expects a regular expression pattern, which provides it more power. You can use this method to say replace all commas with pipe to convert a comma-separated file to a pile delimited String. If you just want to replace one character, just use replace() method, which takes two characters, the old and new characters.

Related Java String Tutorials from Java67 Blog