This article will cover the following content:
- How to Replace a Substring of a String in Java?
- Replace a Single Occurrence of a Substring
- Replace All Occurrences of a Substring
- Replace a Substring Case-Insensitively
How to Replace a Substring of a String in Java?
The syntax for replacing a substring in Java using the replace() method is:
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:
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:
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:
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:
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.