beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java String replace(), replaceFirst() and replaceAll() method

By Chaitanya Singh | Filed Under: String handling

In this tutorial we will discuss replace(), replaceFirst()and replaceAll() methods. All of these Java String methods are mainly used for replacing a part of String with another String.

Java String replace method signature

String replace(char oldChar, char newChar): It replaces all the occurrences of a oldChar character with newChar character. For e.g. "pog pance".replace('p', 'd') would return dog dance.

String replaceFirst(String regex, String replacement): It replaces the first substring that fits the specified regular expression with the replacement String. PatternSyntaxException if the specified regular expression(regex) is not valid.

String replaceAll(String regex, String replacement): It replaces all the substrings that fits the given regular expression with the replacement String.

Java String replace() Method example

In the following example we are have a string str and we are demonstrating the use of replace() method using the String str. We have replaced all the occurrences of char ‘o’ with char ‘p’. In the second print statement we have replaced all the occurrences of char ‘i’ with the char ‘K’.

Here we are displaying the modified string using print statements but we didn’t actually changed the string str, to achieve that we need to assign the returned string of replace() method in a string and then that string can have the permanent changes.

public class JavaExample{
   public static void main(String args[]){
	String str = new String("Site is BeginnersBook.com");

	System.out.print("String after replacing all 'o' with 'p' :" );
	System.out.println(str.replace('o', 'p'));

	System.out.print("String after replacing all 'i' with 'K' :" );
	System.out.println(str.replace('i', 'K'));
   }
}

Output:
Java String replace method example

Java String replaceFirst() Method example

In the following example we are demonstrating the use of replaceFirst() method. This method replaces the part of a string with a new specified string. The difference between replaceFirst() and replaceAll() method is that the replaceFirst() replaces the first occurrence while replaceAll() replaces all the occurrences.

public class JavaExample{
   public static void main(String args[]){
	String str = new String("Site is BeginnersBook.com");

	System.out.print("String after replacing com with net :" );
	System.out.println(str.replaceFirst("com", "net"));

	System.out.print("String after replacing Site name:" );
	System.out.println(str.replaceFirst("Beginners(.*)", "XYZ.com"));
   }
}

Output:
Java String replaceFirst() method example

Java String replaceAll() Method Example

In the following example we are using replaceAll() method to replace all the occurrences of a given substring with the new string.

The difference between replace() and replaceAll() method is that the replace() method replaces all the occurrences of old char with new char while replaceAll() method replaces all the occurrences of old string with the new string. Basically replace() works with replacing chars and replaceAll() works with replacing part of strings.

public class JavaExample{
   public static void main(String args[]){
	String str = new String("My .com site is BeginnersBook.com");
	System.out.print("String after replacing all com with net: " );
	System.out.println(str.replaceAll("com", "net"));
		
	System.out.print("Replacing whole String: " );
	System.out.println(str.replaceAll("(.*)Beginners(.*)", "Welcome"));
   }
}

Output:
Java String replaceAll() method example

❮ PreviousNext ❯

Comments

  1. Chenna Harish says

    September 24, 2015 at 12:36 PM

    i want apostrophe like ‘gugjhg’ and jhjhh’dd these type of insertion in database using string.replace() method in java

    Reply

Leave a Reply Cancel reply

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

Java Tutorials

  • Learn Java
  • OOPs Concepts
  • Java Collections

Java String

  • Java String

Java String Methods

  • String charAt()
  • String compareTo()
  • String compareToIgnoreCase()
  • String contains()
  • String concat()
  • substring
  • String valueOf()
  • String startsWith()
  • String equals()
  • String format()
  • String endsWith()
  • String indexOf()
  • String lastIndexOf()
  • String length()
  • String replace()
  • String split()
  • String trim()
  • String intern()
  • String isEmpty()
  • String matches()
  • String regionMatches()
  • String contentEquals()
  • String toCharArray()
  • String getBytes()
  • String join()
  • String getChars()
  • String copyValueOf()

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap