Hello everyone. This page is an English translation of a Japanese page. (The original Japanese has been slightly rewritten to make it easier to translate into English.)
In this article, we have reviewed the usage of the replace() and replaceAll() methods of the String class.
Differences between the two methods
The replace() and replaceAll() methods are methods for replacing strings. The difference between these methods is whether they can use regular expressions or not. replace() method cannot use regular expressions, while replaceAll can.
The replace() method provides the following two methods
- replace(char oldChar, char newChar)
- replace(CharSequence target, CharSequence replacement)
The usage is like [String type instance].replace('a’, 'b’). One point that is easy to misunderstand is that the original instance string itself will not change. A new instance of type String will always be returned.
The replaceAll() method provides the following one method.
- public String replaceAll(String regex, String replacement)
The first argument is a regular expression.
Example of use
Let’s try to use the method specifically. We chose an appropriate string from the copyright-free English text we used in the previous article. This time, we will use the part “Without farther enumerating or explaining the prizes". Assume that this character is stored in a variable of type String, str. The following works by replacing the letter “a" with the letter “A". The character is enclosed in single quotation marks to specify a char type value as the argument.
String str = "Without farther enumerating or explaining the prizes"; System.out.println(str.replace('a’, 'A’));
Without fArther enumerAting or explAining the prizes
The output is as shown above. The letter “a" appears several times in the original text, but it has all been replaced by “A".
The following works by changing “in" to “IN". The argument is the CharSequence interface, but the String class implements this interface, so you can specify it as an argument.
Without farther enumeratINg or explaININg the prizes
The output is now as shown above.
This time, I used the replaceAll() method.
Let’s assume that the sentence “This is a pen. That is a pen." is stored in a variable of type String. We will consider changing the verb “is" to “is not" in this sentence. If we use the replece() method as follows, the result will not be as expected.
String str = "This is a pen. That is a pen."; System.out.println(str.replace("is", "is not"));
This not is not a pen. That is not a pen.
The “is" in “this" was converted. This is because the word was not taken into account. This time, I will try to use regular expressions in the replaceAll() method. I used the following regular expression to represent whitespace.
System.out.println(str.replaceAll("\\sis\\s", " is not "));
This is not a pen. That is not a pen.
The result is as expected. The only thing to note is that the spaces before and after “is" are also replaced. For this reason, we put spaces before and after “is not" in the second argument.
That’s all. I hope this is helpful to you.