Home » Core Java » lang » String » Java String replaceAll example

About Nikos Maravitsas

Nikos Maravitsas
Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

Java String replaceAll example

In this example we are going to see how to use replaceAll String class API method. With replaceAll, you can replace all character sequences and single characters from a String instance.

As you know, String objects are immutable. This means, that every time you try to change its value, a new String object is created, holding the new value. So replaceAll will create a new String object and return a reference pointing to it.

Let’s see some examples.
 
 

1. Replacing single characters

Here is how you can replace all instances of a single character inside a String.

StringReplaceAllExample.java:

package com.javacodegeeks.core.string;

public class StringReplaceAllExample {

	public static void main(String[] args) {

		String str = "This is a String to use as an example to present raplaceAll";

		// replace all occurrences of 'a' with '@'
		String newStr = str.replaceAll("a", "@");
		System.out.println(newStr);

		// replace all occurrences of 'e' with '3'
		newStr = newStr.replaceAll("e", "3");
		System.out.println(newStr);

		// replace all occurrences of 't' with 'T'
		newStr = newStr.replaceAll("t", "T");
		System.out.println(newStr);

		// remove all occurrences of 'o'
		newStr = newStr.replaceAll("o", "");
		System.out.println(newStr);

		// replace all occurrences of 't' with 'That'
		newStr = newStr.replaceAll("T", "That");
		System.out.println(newStr);
	}
}

This will output :

This is @ String to use @s @n ex@mple to present r@pl@ceAll
This is @ String to us3 @s @n 3x@mpl3 to pr3s3nt r@pl@c3All
This is @ STring To us3 @s @n 3x@mpl3 To pr3s3nT r@pl@c3All
This is @ STring T us3 @s @n 3x@mpl3 T pr3s3nT r@pl@c3All
Thathis is @ SThatring That us3 @s @n 3x@mpl3 That pr3s3nThat r@pl@c3All

As you can see from the above example, replacement is a case sensitive operation, For example, when we replaced all occurrences of ‘a’, we didn’t replace ‘A’ in the original String. Additionally, you can use replaceAll to completely remove a character from the String by replacing it with "". It’s also worth noting that if no occurrences of the targeted character are found, as a result replaceAll will return the exact same String, no new String gets created.

2. Replacing char sequences character

Here is how you can replace all char sequences inside a String with another char sequence or with a single character.

StringReplaceAllExample.java:

package com.javacodegeeks.core.string;

public class StringReplaceAllExample {

	public static void main(String[] args) {

		String str = "This is a String to use as an example to present raplaceAll";

		// replace all occurrences of 'This' with 'That'
		String newStr = str.replaceAll("This", "That");
		System.out.println(newStr);

		// replace all occurrences of 'String' with 'big String'
		newStr = str.replaceAll("String", "big String");
		System.out.println(newStr);

		// remove all occurrences of 'is'
		newStr = str.replaceAll("is", "");
		System.out.println(newStr);

		// remove all occurrences of 'replaceAll'
		newStr = str.replaceAll("raplaceAll", "");
		System.out.println(newStr);
	}
}

This will output :

That is a String to use as an example to present raplaceAll
This is a big String to use as an example to present raplaceAll
Th  a String to use as an example to present raplaceAll
This is a String to use as an example to present 

3. Using regular expressions

This is a great feature of replaceAll. You can use a regular expression to replace all occurrences of matching char sequences inside a String.

StringReplaceAllExample.java:

package com.javacodegeeks.core.string;

public class StringReplaceAllExample {

	public static void main(String[] args) {

		String str = "This 1231 is 124 a String 1243 to 34563 use 5455";

		// remove all numbers
		String newStr = str.replaceAll("[0-9]+", "");
		System.out.println(newStr);

		// remove all words with 'Java'
		newStr = str.replaceAll("[a-zA-Z]+", "Java");
		System.out.println(newStr);
	}
}

This will output :

This  is  a String  to  use 
Java 1231 Java 124 Java Java 1243 Java 34563 Java 5455

Download Source Code

This was an example on Java String replaceAll. Download the source code of this example here : StringReplaceAllExample.zip

(No Ratings Yet)
1 Comment Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
santhoshi Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
santhoshi
Guest
santhoshi

I want to replace Name with name in the Text but if i use replace method it is replacing LastName also with Lastname. But i want only Name only should be replaced.