Java String replaceAll() Method Last Updated : 23 Dec, 2024 Comments Improve Suggest changes Like Article Like Report String replaceAll method in Java searches for a specified string or regex pattern and returns a new string with the matched characters replaced.Example 1: Java class Geeks { public static void main(String args[]) { String str = new String("Welcome to geeksforgeeks"); // Using replaceAll to replace regex with // replace_str System.out.print( "After replacing regex with" + " replace_str : "); System.out.println(str.replaceAll("(.*)geeks(.*)", "GFG")); } } OutputAfter replacing regex with replace_str : GFG This method replaces each substring of the string that matches the given regular expression with the given replace_str. Syntaxpublic String replaceAll(String regex, String replace_str)Parameters:regex: the regular expression to which this string is to be matched.replace_str: the string which would replace found expression.Return Value: This method returns the resulting String.Example 2: Invalid regex when passed in raplaceAll() method, raises PatternSyntaxException. Java import java.io.*; class Geeks { public static void main(String[] args) { String str = "GFG"; // Incorrect Regular expression String regex = "\\"; // Passing null expression in // replaceAll method str = str.replaceAll(regex, " "); System.out.println(str); } } OutputException in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1 at java.base/java.util.regex.Pattern.error(Pattern.java:2027) at java.base/java.util.regex.Pattern.compile(Pattern.java:1788) at java.base/java.util.regex.Pattern.<init>(Pattern.java:1428) at java.base/java.util.regex.Pattern.compile(Pattern.java:1068) at java.base/java.lang.String.replaceAll(String.java:2126) at GFG.main(GFG.java:11)Example 3: The null regular expression is not accepted by the replaceAll() method, it raises the NullPointerException. Java import java.io.*; class Geeks { public static void main(String[] args) { // input string String str = "GFG"; // NULL Regular expression String regex = null; // Passing null expression in // replaceAll method str = str.replaceAll(regex, " "); System.out.println(str); } } OutputException in thread "main" java.lang.NullPointerException at java.base/java.util.regex.Pattern.<init>(Pattern.java:1426) at java.base/java.util.regex.Pattern.compile(Pattern.java:1068) at java.base/java.lang.String.replaceAll(String.java:2126) at GFG.main(GFG.java:11) Comment More infoAdvertise with us Next Article Java String replaceAll() Method A Astha Tyagi Follow Improve Article Tags : Misc Java Java-Strings Java-lang package Practice Tags : JavaJava-StringsMisc Similar Reads C# String Replace() Method In C#, the Replace() method is used to replace all occurrences of a specified substring or character in a string with another substring or character. This method is particularly useful for string manipulation tasks, such as formatting or cleaning up data.Example 1: Using Replace() method to replace 3 min read Python String replace() Method The replace() method replaces all occurrences of a specified substring in a string and returns a new string without modifying the original string.Letâs look at a simple example of replace() method.Pythons = "Hello World! Hello Python!" # Replace "Hello" with "Hi" s1 = s.replace("Hello", "Hi") print( 2 min read Set removeAll() Method in Java In Java, the removeAll() method is part of the Collection interface. It is used to remove all elements from a collection that are present in another collection.Example 1: This example demonstrates how the removeAll() method removes all elements from the first set that are also present in the second 2 min read String replace() method in Java with Examples The String replace() method returns a new string after replacing all the old characters/CharSequence with a given character/CharSequence. Example:Return a new string where all " o" characters are replaced with "p" character: Java // Java program to demonstrate // the replace() method public class Ma 4 min read Matcher replaceAll(String) method in Java with Examples The replaceAll(String) method of Matcher Class behaves as a append-and-replace method. This method reads the input string and replace it with the matched pattern in the matcher string. Syntax: public String replaceAll(String stringToBeReplaced) Parameters: This method takes a parameter stringToBeRep 2 min read Vector removeAll() Method in Java The java.util.vector.removeAll(Collection col) method is used to remove all the elements from the vector, present in the collection specified. Syntax: Vector.removeAll(Collection col) Parameters: This method accepts a mandatory parameter col which is the collection whose elements are to be removed f 2 min read Java String trim() Method The trim() method of the String class in Java is used to remove leading and trailing whitespace from a string. The Unicode value of the space character is "\u0020". It does not remove any whitespace in the middle of the string. This method returns a new string with the whitespace removed and leaves 4 min read Java HashMap replace() Method The replace() method of the HashMap class in Java is used to replace the value associated with a specific key if the key is already present in the map.Note: If the key does not exist, the method does nothing and the map remains unchanged.Example 1: This example demonstrates replacing the value of an 3 min read Matcher replaceFirst(String) Method in Java with Examples The replaceFirst() method of Matcher Class behaves as an append-and-replace method. This method reads the input string and replaces it with the first matched pattern in the matcher string. Syntax: public String replaceFirst(String stringToBeReplaced) Parameters: The string to be replaced that is the 2 min read Stack removeAll() method in Java with Example The Java.util.Stack.removeAll(Collection col) method is used to remove all the elements from the Stack, present in the collection specified. Syntax: Stack.removeAll(Collection col) Parameters: This method accepts a mandatory parameter col which is the collection whose elements are to be removed from 2 min read Like