Java String replaceAll() method

Java String ReplaceAll

Well, with this method, we can replace some regular expressions with some replacement strings. We can replace some old characters with some new characters as well, similar to what we just did above. The thing is that we can do a lot more here.

Here is a program to do the same thing as we did above with the help of replace method –

public class StringMethods {
public static void main(String[] args) {
String str1 = “This food house serves good food”;
System.out.println(str1.replaceAll(“o”, “u”));
}
}

We just did the same thing, just with the use of the replaceAll method. If you try executing the program, you can observe the output to be a string with all the occurrences of the old character ‘o’, with the replacement character ‘u’.

With this replaceAll method, we can also get a word replaced by another word. Like if we try replacing all the occurrences of the word ”good” with the word ”tasty”, we can comfortably do that here. Have a look at the below program where we are trying to do the same thing –

public class StringMethods {
public static void main(String[] args) {
String str1 = “This food house serves good food”;
System.out.println(str1.replaceAll(“good”, “tasty”));
}
}

Now if we try executing the program, we can find out that all the occurrences of the word ”good” are replaced with the word ”tasty”. Well, we just had one occurrence of that word.

Alternatively, you can try replacing the word ”food”, with the word ”snacks”. This makes sense as well, and there is more than one occurrence for the word food, so you can give it a try.

Let’s also see something more. If you are familiar with something called a regular expression, it is too good, but if you are not, just don’t worry, because we are going to discuss it quite in brief here, just so that we can understand our next program.

A regular expression is just a set of characters specifying a search pattern. Just imagine a situation in which you want to search for all the words which start with the letter ‘a in your string. What are we going to do in such a situation? One choice would be just checking the first character from every word in the string, and comparing if that character is ‘a’.

Or another thing that we can do, is that we have to check each word in the string and see whether it starts with the given character (this can be done with the startsWith method, which is going to be covered later). But along with some of these things, Regular expression can also be used here.

The thing is that with regular expression, we get a pattern through which we can search our string for example. There can be some regular expression, which helps you match a string that starts with the letter ‘a’. We are not going into complete details of regular expressions, since it is a completely different topic, but you can explore it best on your level, and if we are using something, we will first understand the thing and then get ahead.

If we put some invalid regular expression in the above codes explaining the replaceAll method, it throws an exception, which is PatternSyntaxException.

Let’s now have a look at the below example, where we are trying to make use of the regular expression here so that you can get quite familiar with the concept of regular expressions.

public class Sample {
public static void main(String[] args) {
String myString = “The number 6 is greater than 4 and less than 9”;
System.out.println(myString.replaceAll(“[0-9]”, “@”));
}
}

Well, as you can see, we have taken a regular expression here, which just specifies that we have to consider some value from 0 to 9. If you observe in the string, there are some numbers like 6, 4, and 9, which fall within the range. So, they are going to be considered for replacement. So, we are just saying that replace any number from 0 to 9, with an @ symbol. Remember that I am just printing here so that we can understand the output, but the replaceAll method returns a string with the effective changes.

Well, as specified earlier, if we are putting some invalid regular expression, then the result is going to be an exception(PatternSyntaxException). So, let’s try putting some invalid regular expressions (like ”[”). have a look at the below example –

public class Sample {
public static void main(String[] args) {
String myString = “The number 6 is greater than 4 and less than 9”;
System.out.println(myString.replaceAll(“[“, “@”));
}
}

If you try to run the above program, it is going to raise an exception. You can try playing with this, try some more regular expressions(which of course you can explore), and try to observe the output which you get using the regular expression.