Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
Java strings are immutable, which means that once they are created, their value cannot be changed. However, Java provides several methods to create new strings with modified content. In this tutorial, we will cover three common methods to replace characters or substrings in Java strings: using the replace()
method, the replaceFirst()
method, and the replaceAll()
method.
Method 1: Using the replace()
method
The replace()
method of the String
class can be used to replace all occurrences of a specific character or substring with another character or substring.
Example:
public class Main { public static void main(String[] args) { String input = "Hello World! This is an example."; String output = input.replace("example", "demo"); System.out.println("Original string: " + input); System.out.println("Replaced string: " + output); } }
Method 2: Using the replaceFirst()
method
The replaceFirst()
method of the String
class can be used to replace the first occurrence of a specific substring with another substring. The method takes a regular expression pattern as an argument, so you must escape any characters that have special meaning in regular expressions.
Example:
public class Main { public static void main(String[] args) { String input = "Hello World! This is an example. Another example."; String output = input.replaceFirst("example", "demo"); System.out.println("Original string: " + input); System.out.println("Replaced string: " + output); } }
Method 3: Using the replaceAll()
method
The replaceAll()
method of the String
class can be used to replace all occurrences of a substring with another substring using a regular expression pattern. This method is more powerful than the replace()
method, as it allows you to use regular expressions for pattern matching and replacement.
Example:
public class Main { public static void main(String[] args) { String input = "Hello World! This is an example. Another example."; String output = input.replaceAll("ex(ample)", "de$1"); System.out.println("Original string: " + input); System.out.println("Replaced string: " + output); } }
In this example, the regular expression pattern ex(ample)
matches the substring "example", and the replacement string de$1
replaces the matched substring with "demo". The $1
in the replacement string refers to the first capturing group in the pattern (the part of the pattern enclosed in parentheses).
In this tutorial, we covered three methods to replace characters or substrings in Java strings. These methods can be used depending on your preference or the specific requirements of your application. Keep in mind that all these methods return a new string with the replaced content, as the original string remains unchanged due to its immutability.
Replacing Characters in Java Strings:
replace()
to replace specific characters in a string.String replaced = inputString.replace('a', 'b');
Using replaceFirst()
in Java Strings:
replaceFirst()
replaces the first occurrence of a character or substring.String replacedFirst = inputString.replaceFirst("a", "b");
Java replaceAll()
Method Examples:
replaceAll()
replaces all occurrences using regex or string.String replacedAll = inputString.replaceAll("a", "b");
Replacing Specific Characters in Java Strings:
replace()
.String replacedSpecific = inputString.replace("abc", "123");
Replacing Substrings in Java using replace()
:
replace()
to replace substrings.String replacedSubstring = inputString.replace("oldSubstring", "newSubstring");
Case-Insensitive Replacement in Java Strings:
replaceAll()
.String replacedIgnoreCase = inputString.replaceAll("(?i)old", "new");
Replacing Multiple Occurrences in Java Strings:
replace()
.String replacedMultiple = inputString.replace("a", "b");
Java Regex in replaceAll()
Method:
replaceAll()
for advanced replacements.String replacedRegex = inputString.replaceAll("\\d", "");
Replacing Newline Characters in Java Strings:
String replacedNewline = inputString.replace("\n", " ");
Replacing Spaces with Underscores in Java:
replace()
.String replacedSpaces = inputString.replace(" ", "_");
Replace Certain Characters with Escape Sequences in Java:
String replacedEscape = inputString.replace("\t", "\\t");
Conditional Replacement in Java Strings:
String replacedConditionally = (condition) ? inputString.replace("a", "b") : inputString;
Replacing Special Characters in Java Strings:
replace()
or replaceAll()
.String replacedSpecial = inputString.replace("@", "_at_");