Java String replaceAll()

The replaceAll() method in Java is used to replace all occurrences of a particular character or substring in a given string with a new character or substring.

The syntax for using the replaceAll() method is:

public String replaceAll(String regex, String replacement)

where regex is the regular expression (pattern) to be replaced, and replacement is the string that will replace the matched pattern.

Here is an example:

String str = "Hello, World!";
String newStr = str.replaceAll("o", "e");
System.out.println(newStr); // "Helle, Werld!"

In this example, the replaceAll() method replaces all occurrences of the character “o” with the character “e” in the original string.

Note that the replaceAll() method uses regular expressions, so you can use regular expression patterns to match more complex patterns in the string. For example, you can use the pattern "\\d" to match all digits in a string and replace them with a new character or substring.

Signature of Java String replaceAll():

The signature of the replaceAll() method in Java is:

public String replaceAll(String regex, String replacement)

The replaceAll() method takes two parameters:

  • regex: a regular expression that specifies the pattern to match in the string
  • replacement: the string that will replace the matched pattern

The method returns a new string that is the result of replacing all occurrences of the specified pattern with the replacement string.

Note that the replaceAll() method is case-sensitive, meaning that it will only replace occurrences of the pattern that match exactly. If you want to perform a case-insensitive replacement, you can use the replaceAllIgnoreCase() method instead.

Parameters of Java String replaceAll():

The replaceAll() method in Java takes two parameters:

  1. regex: A regular expression that specifies the pattern to match in the string. This can be a simple string or a more complex regular expression pattern. For example, the pattern "o" will match all occurrences of the character “o” in the string, while the pattern "\\d" will match all occurrences of digits.
  2. replacement: The string that will replace the matched pattern. This can be a simple string or a more complex replacement pattern. For example, the replacement string "e" will replace all occurrences of the matched pattern with the character “e”, while the replacement string "-$0-" will insert a hyphen before and after each matched pattern.

Both the regex and replacement parameters are of type String.

When the replaceAll() method is called, it returns a new string that is the result of replacing all occurrences of the specified pattern with the replacement string. Note that the original string is not modified, as strings are immutable in Java.

Returns of Java String replaceAll():

The replaceAll() method in Java returns a new string that is the result of replacing all occurrences of the specified pattern with the replacement string.

For example, consider the following code snippet:

String str = "Hello, world!";
String newStr = str.replaceAll("o", "e");
System.out.println(newStr);

The output of this code will be:

Helle, werld!

In this example, the replaceAll() method replaces all occurrences of the character “o” in the original string with the character “e”, and returns a new string with the modified content. The original string str remains unchanged.

Note that if the specified pattern does not match any part of the original string, the replaceAll() method will return a new string that is identical to the original string.

Internal implementation:

The internal implementation of the replaceAll() method in Java is not specified in the Java API documentation, as it is an implementation detail of the Java runtime. However, the replaceAll() method is typically implemented using regular expression matching and substitution.

When the replaceAll() method is called, the Java runtime first compiles the regular expression pattern specified in the regex parameter into a pattern object using the Pattern.compile() method. It then uses this pattern object to search for all occurrences of the specified pattern in the original string.

Once all occurrences of the pattern have been located, the replaceAll() method replaces each occurrence with the replacement string specified in the replacement parameter. The resulting modified string is then returned as the result of the replaceAll() method.

It is worth noting that the replaceAll() method returns a new string object with the modified content, as strings are immutable in Java. This means that the original string is not modified, and a new string object is created to hold the modified content.

Java String replaceAll() example: replace character:

Sure, here is an example of using the replaceAll() method in Java to replace a character in a string:

String str = "Hello, world!";
String newStr = str.replaceAll("o", "e");
System.out.println(newStr);

In this example, we first create a string str that contains the text “Hello, world!”. We then use the replaceAll() method to replace all occurrences of the character “o” with the character “e” in the string. The resulting modified string is stored in the newStr variable, and is printed to the console using the println() method.

The output of this code will be:

Helle, werld!

As you can see, all occurrences of the character “o” in the original string have been replaced with the character “e”, resulting in the modified string “Helle, werld!”.

Java String replaceAll() example: replace word:

Sure, here’s an example of using the replaceAll() method in Java to replace a word in a string:

String str = "The quick brown fox jumps over the lazy dog.";
String newStr = str.replaceAll("fox", "cat");
System.out.println(newStr);

In this example, we first create a string str that contains the text “The quick brown fox jumps over the lazy dog.”. We then use the replaceAll() method to replace all occurrences of the word “fox” with the word “cat” in the string. The resulting modified string is stored in the newStr variable, and is printed to the console using the println() method.

The output of this code will be:

The quick brown cat jumps over the lazy dog.

As you can see, all occurrences of the word “fox” in the original string have been replaced with the word “cat”, resulting in the modified string “The quick brown cat jumps over the lazy dog.”. Note that the replaceAll() method is case-sensitive, so it will only replace exact matches of the specified word. If you want to perform a case-insensitive replacement, you can use the replaceAllIgnoreCase() method instead.

Java String replaceAll() example: remove white spaces:

Sure, here’s an example of using the replaceAll() method in Java to remove white spaces from a string:

String str = "  The quick    brown fox   jumps over    the lazy dog.  ";
String newStr = str.replaceAll("\\s+", "");
System.out.println(newStr);

In this example, we first create a string str that contains the text ” The quick brown fox jumps over the lazy dog. “. We then use the replaceAll() method with a regular expression pattern \\s+ to replace all one or more consecutive white spaces with an empty string. The resulting modified string is stored in the newStr variable, and is printed to the console using the println() method.

The output of this code will be:

Thequickbrownfoxjumpsoverthelazydog.

As you can see, all white spaces in the original string have been removed, resulting in the modified string “Thequickbrownfoxjumpsoverthelazydog.”. Note that the regular expression pattern \\s+ matches one or more consecutive white spaces, tabs, or line breaks, so it removes all kinds of white spaces from the string.