• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • 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)
  • 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)

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

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);

	}
}

Output…

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

Category: JavaTag: 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.

Previous Post: « Java String Intern Method Example
Next Post: Java String Reverse using StringBuffer Example »

Reader Interactions

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.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact