AppDividend
Latest Code Tutorials

Java String replaceAll Example | replaceAll() Function In Java

0

The Java String replaceAll function returns the string after it replaces each substring of that matches a given regular expression with a given replacement. Sometimes, it is required to replace more than just one character or one character sequence in a given String, and instead do the replacement on a larger part of the String.

Java String replaceAll

Java String has three types of Replace method

  1. replace
  2. replaceAll
  3. replaceFirst.

We will see replaceAll() function. 

The Java.lang.String.replaceAll() method takes in a regular expression (regex) and a replacement String as parameters and returns a string by replacing each substring of the initial string that matches the given regex with the replacement String. See the following method.

 

Java String replaceAll

See the following syntax.

public String replaceAll(String regex, String replacement)

Parameters: 

String regex – The String is matched to this regex.

String replacement – The matched regex is replaced with this replacement.

Returns:

The String after replacing each substring that matches the regex with the replacement.

Internal Implementation:

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

Examples: The starting examples demonstrate the similarities of the replaceAll() method with that of replace() method. The examples that follow describe the unique applicabilities of the replaceAll() method.

See the following method.

The following example demonstrates how the replaceAll() method can be used to change all occurrences of an old character with a new character, similar to that done by the replace() method.

public class Example1 {
	public static void main(String[] args) {
		String s1 = new String("aaa");
		System.out.println(s1);

		String s2 = new String();
		s2 = s1.replaceAll("a", "z");

		System.out.println(s2);
	}
}

Output

aaa
zzz

Example2.java:

The following example demonstrates how the replaceAll() method can be used to replace all occurrences of an old character sequence with a new one, similar to that done by the replace() method.

public class Example2 {
	public static void main(String[] args) {
		String s1 = new String("You don't know who I am. You clearly don't know who I am.");
		System.out.println(s1);

		String s2 = new String();
		s2 = s1.replaceAll("I am", "Heisenberg is");

		System.out.println(s2);
	}
}

Output

You don’t know who I am. You clearly don’t know who I am.
You don’t know who Heisenberg is. You clearly don’t know who Heisenberg is.

Example3.java

The following examples describes the use of the replaceAll() method in manipulating Strings using regex in different possible ways.

public class Example3 {
	public static void main(String[] args) {
		String s1 = new String("War of the worlds.");
		System.out.println(s1);

		String s2 = new String();
		s2 = s1.replaceAll("(.*)the", "Hello,");
		System.out.println(s2);
	}
}

Output

War of the worlds.
Hello, worlds.

Example4.java

public class Example4 {
	public static void main(String[] args) {
		String s1 = new String("A new world order.");
		System.out.println(s1);

		String s2 = new String();
		s2 = s1.replaceAll("world(.*)", "border.");
		System.out.println(s2);
	}
}

Output

A new world order.
A new border.

Example5.java

public class Example5 {
	public static void main(String[] args) {
		String s1 = new String("I am an old old man.");
		System.out.println(s1);
		String s2 = new String();
		s2 = s1.replaceAll("old(.*)", "new man.");
		System.out.println(s2);
	}
}

Output

I am an old old man.
I am a new man.

Example6.java

public class Example6 {
	public static void main(String[] args) {
		String s1 = new String("I am a old old man.");
		System.out.println(s1);

		String s2 = new String();
		s2 = s1.replaceAll("(.*)old", "new man.");
		System.out.println(s2);
	}
}

Output

I am an old old man.
new man. man.

These are the various ways in which the replaceAll() method can be used to manipulate Strings and take in a regular expression (regex) and a replacement String as parameters and return the String by replacing each substring of the initial string that matches the given regex with the replacement String.

throws PatternSyntaxException

Watch out that replaceAll() throws PatternSyntaxException if the regular expression’s syntax is invalid. In the given an example, “[” is an invalid regular expression.

public class Example7 {
	public static void main(String[] args) {
		String newStr = "krunal lathiya".replaceAll("[", "");
	}
}

Output

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0
[
^
    at java.util.regex.Pattern.error(Pattern.java:1955)
    at java.util.regex.Pattern.clazz(Pattern.java:2548)
    at java.util.regex.Pattern.sequence(Pattern.java:2063)
    at java.util.regex.Pattern.expr(Pattern.java:1996)
    at java.util.regex.Pattern.compile(Pattern.java:1696)
    at java.util.regex.Pattern.<init>(Pattern.java:1351)
    at java.util.regex.Pattern.compile(Pattern.java:1028)
    at java.lang.String.replaceAll(String.java:2210)
    at com.StringExample.main(StringExample.java:9)

Finally, Java String replaceAll Example | replaceAll() Function In Java Tutorial is over.

Recommended Posts

Java String Format Example

Java String getBytes() Example

How To Convert String Characters To Lowercase

Java String toCharArray() Example

Java String indexOf() Example

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.