JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

Java String ReplaceAll Method Example

April 6, 2014 by Krishna Srinivasan Leave a Comment

In this example we shall explain the replaceAll() method in the String class. With the replaceAll() method we can replace the whole or part of the string or character sequences. String objects are immutable and cannot be changed the value. When ever you change the value, new string object is created. This method also creates the new object after replacing the values and return the new object. Lets look at the below example to understand this concept better. Some of the operations you can do using the replaceAll() method is:

  1. You can replace the single character
  2. You can replace the sequence of characters or a word
  3. You can replace using the regular expression

StringReplaceExample.java

[code lang=”java”]
package javabeat.net.core;

/**
* String replaceAll() method example
* @author krishna
*
*/
public class StringReplaceExample {
public static void main(String args[]){
String str = "String replaceAll() method example!!";
String strRegExTest = "Java 12 23 String 4 Replace Example";
String strObj = null;

// Replace all occurrences of "t" to "T"
strObj = str.replaceAll("t", "T");
System.out.println(strObj);

// Remove all occurrences of "!"
strObj = str.replaceAll("!", "");
System.out.println(strObj);

// Replace "example" to "Example"
strObj = str.replaceAll("example", "Example");
System.out.println(strObj);

// Remove all the numbers
strObj = strRegExTest.replaceAll("[0-9]+", "");
System.out.println(strObj);

// Replace all the words to "Word"
strObj = strRegExTest.replaceAll("[a-zA-Z]+", "Word");
System.out.println(strObj);

}
}
[/code]

Output…

[code]
STring replaceAll() meThod example!!
String replaceAll() method example
String replaceAll() method Example!!
Java String Replace Example
Word 12 23 Word 4 Word Word
[/code]

Filed Under: Java Tagged With: Java String

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

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

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved