Java

How to Replace a Substring of a String in Java?

In Java, the replace() or replaceAll() methods of the String class allows users to replace a substring of a string with another one. The “replace()” method replaces all instances of a particular substring with another substring, while the “replaceAll()” method replaces all instances of a specified regular expression with another substring.

This article will cover the following content:

How to Replace a Substring of a String in Java?

The syntax for replacing a substring in Java using the replace() method is:

String newString = originalString.replace(oldSubstring, newSubstring);

Here,

  • The “originalString” is the actual string that is ready for modification.
  • The “oldSubstring” is the substring for replacement.
  • The “newSubstring” is the substring that will replace the old one.
  • The “replace()” method is utilized to return a new string.

If users want to replace all occurrences of the old substring, you can use the replaceAll() method instead:

String newString = originalString.replaceAll(oldSubstring, newSubstring);

The “replaceAll()” method works similarly to the “replace()” method, but it replaces all occurrences of the old substring instead of just the first one.

Here are three examples that demonstrate diverse ways to replace a substring of a string in Java:

Example 1: Replace a Single Occurrence of a Substring

Here is an example that demonstrates how to use the replace() method to replace a substring of a string:

class Work {
    public static void main(String[] args) {
      String originalString = "The quick brown fox jumps over the lazy dog.";
      String newString = originalString.replace("fox", "cat");
      System.out.println(newString);
    }
}

In this example,

  • First, create a string called “originalString” that contains the phrase “The quick brown fox jumps over the lazy dog.”
  • After that, the “replace()” method is utilized for replacing all instances of the substring “fox” with the substring “cat“, and storing the result in a string named “newString”.
  • Finally, print the new string to the console, which will output “The quick brown cat jumps over the lazy dog.”.

In this example, replace the first occurrence of “fox” in the original string with “cat” using the replace() method.

Example 2: Replace All Occurrences of a Substring

Another example is considered to replace all occurrences of a substring:

String originalString = "The quick brown fox jumps over the lazy dog.";

String newString = originalString.replaceAll("o", "0");

System.out.println(newString);

In the above code snippets, replace all instances of the “o” letter in the original string with the number “0” using the replaceAll() method.

The output shows that the “o” letter is replaced with the “0” number.

Example 3: Replace a Substring Case-Insensitively

In this example, replace all occurrences of the substring “fox” in the original string case-insensitively with “cat” using the “replaceAll()” method and a regular expression:

class Work {
    public static void main(String[] args) {
     String originalString = "The quick brown fox jumps over the lazy dog.";
     String newString = originalString.replaceAll("(?i)fox", "cat");
     System.out.println(newString);
    }
}

The description of the above code is given below:

  • In the regular expression, (?i) is a flag that indicates that the matching should be case-insensitive.
  • The “replaceAll()” method uses this regular expression to find and replace all occurrences of “fox” in the original string with “cat”, regardless of whether the letters are upper or lowercase.

Output

The output confirms that all occurrences of “fox” have been replaced in the original string with “cat”.

Conclusion

To replace a single occurrence of a substring in a string in Java, use the “replace()” method. Also, the “replaceAll()” method replaces all instances of a substring. Both these methods return a new string and do not modify the actual string. If users want to replace a substring case insensitively, use a regular expression with the “replaceAll()” method.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.