Site icon TechVidvan

Java String replaceAll() Method

java string replaceAll()

Java’s String replaceAll function creates a new string with characters that are related to a supplied string, a defined value, or a regex expression.

Explanation

The replaceAll() method of the Java String class returns a string that replaces all the characters in the regex and replacement string sequence.

Each substring in the string that matches the regex is replaced with the supplied text using the replaceAll() method.

Syntax:

public String replaceAll(String regex, String replace_str)

Internal implementation

public String replaceAll(String regex, String replacement) {  
        return
Pattern.compile(regex).matcher(this).replaceAll(replacement);  
}

Parameter:

Return Value:

Diagram:

Example:

For instance, to replace all occurrences of a single character:

public class TechVidvanReplaceAll{  
public static void main(String args[]){  
String s1="Good things takes time";  
String replaceString=s1.replaceAll("o","e");
System.out.println(replaceString);  
}
}

Output:
Good things takes time

Example:

For example, to replace all occurrences of a single word or collection of terms:

public class TechVidvanReplaceAll{  
public static void main(String args[]){  
String s1="My name is Jaanu, her name is Nithu.";  
String replaceString=s1.replaceAll("is","was");  
System.out.println(replaceString); 
}
}

Output:
My name was Jaanu, her name was Nithu.

Example:

Inserting spaces between characters was done by using the replaceAll() method:

public class ReplaceAllExample5   
{  
// main method  
public static void main(String argvs[])  
{  
  
// input string  
String str = "Cricket";  
System.out.println(str);  
  
String regex = "";  
// adding a white space before and after every character of the input string.  
str = str.replaceAll(regex, " ");  
  
System.out.println(str);  
  
}  
}

Output:
Cricket
C r i c k e t

Escaping Characters in replaceAll()

Whether it is a regex or a regular string, the first argument to the replaceAll() method is up to you, mainly because a regular expression is a typical string.

Regex contains characters with special meanings.

The metacharacters are as follows:

\ ^ $ . | ? * + {} [] ()

Use either the method or the replace() method to match the substring that contains metacharacters.

Difference between replaceAll() and replace()

How to replace all characters?

Special Characters

Exception of String replaceAll()

Although the String replaceAll method in Java is quite effective, it does have some limitations.

The four primary ways to handle this exception:

public String getMessage() The description of the exception is included.
public int getIndex() It will return the error of the index.
public String getPattern() It will return the regular expression that contains the error.
public String getDescription() It will provide us with a decryption for the error.

Usage of string replaceAll()

Advantages of String replaceAll()

Disadvantages of String replaceAll()

Conclusion

We have discussed the String replaceAll() in Java. In extensively learned about its uses and function of replaceAll().

Exit mobile version