Java String replace(), replaceFirst() & replaceAll() Methods



The replace() method

The replace() method of the String class accepts two String values −

  • One representing the part of the String (substring) to be replaced.

  • Another one representing the String with which you need to replace the specified substring.

Using this method, you can replace a part of a string in java.

Example

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReplaceExample {
   public static void main(String args[]) throws FileNotFoundException {
      String filePath = "D://input.txt";
      Scanner sc = new Scanner(new File(filePath));
      StringBuffer sb = new StringBuffer();
      String input;
      while (sc.hasNextLine()) {
         input = sc.nextLine();
         sb.append(input);
      }
      String contents = sb.toString();
      System.out.println("Contents of the file: \n"+contents);
      System.out.println(" ");
      //Replacing the word with desired one
      contents = contents.replace("Tutorialspoint", "TP");
      System.out.println("Contents of the file after replacing the desired word: \n"+contents);
   }
}

Output

Contents of the file:
Hello how are you welcome to Tutorialspoint. At Tutorialspoint provide hundreds of technical tutorials for free.

Contents of the file after replacing the desired word:
Hello how are you welcome to TP. At TP provide hundreds of technical tutorials for free.

Another variant of this method also accepts two characters, representing existing character and one character respectively (in the same order) and, replaces the old character with the new one in the whole string.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReplaceExample {
   public static void main(String args[]) throws FileNotFoundException {
      String filePath = "D://input.txt";
      Scanner sc = new Scanner(new File(filePath));
      StringBuffer sb = new StringBuffer();
      String input;
      while (sc.hasNextLine()) {
         input = sc.nextLine();
         sb.append(input);
      }
      String contents = sb.toString();
      System.out.println("Contents of the file: \n"+contents);
      System.out.println(" ");
      //Replacing the word with desired one
      contents = contents.replace('T', '#');
      System.out.println("Contents of the file after replacing the desired word: \n"+contents);
   }
}

Output

Contents of the file:
Hello how are you welcome to Tutorialspoint. At Tutorialspoint provide hundreds of technical tutorials for free.

Contents of the file after replacing the desired word:
Hello how are you welcome to #utorialspoint. At #utorialspoint provide hundreds of technical tutorials for free.

The replaceAll() method

The replaceAll() method of the String class accepts two strings representing a regular expression and a replacement pattern/string and, replaces the matched values with specified pattern/string.

Example

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class replaceAllExample {
   public static void main(String args[]) throws FileNotFoundException {
      String filePath = "D://input.txt";
      Scanner sc = new Scanner(new File(filePath));
      StringBuffer sb = new StringBuffer();
      String input;
      while (sc.hasNextLine()) {
         input = sc.nextLine();
         sb.append(input);
      }
      String contents = sb.toString();
      System.out.println("Contents of the file: \n"+contents);
      System.out.println();
      //Replacing the word with desired one
      contents = contents.replaceAll("\bTutorialspoint\b", "TP");
      System.out.println("Contents of the file after replacing the desired word: \n"+contents);
   }
}

Output

Contents of the file:
Hello how are you welcome to Tutorialspoint. At Tutorialspoint provide hundreds of technical tutorials for free.

Contents of the file after replacing the desired word:
Hello how are you welcome to TP. At TP provide hundreds of technical tutorials for free.

The replaceFirst() method

The replaceFirst() method of the String class (also) replaceFirst accepts two strings representing a regular expression and a replacement string and, replaces the first match with the replacement string.

Example

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReplaceExample {
   public static void main(String args[]) throws FileNotFoundException {
      String filePath = "D://input.txt";
      Scanner sc = new Scanner(new File(filePath));
      StringBuffer sb = new StringBuffer();
      String input;
      while (sc.hasNextLine()) {
         input = sc.nextLine();
         sb.append(input);
      }
      String contents = sb.toString();
      System.out.println("Contents of the file: \n"+contents);
      System.out.println(" ");
      //Replacing the word with desired one
      contents = contents.replaceFirst("Tutorialspoint", "TP");
      System.out.println("Contents of the file after replacing the desired word: \n"+contents);
   }
}

Output

Contents of the file:
Hello how are you welcome to Tutorialspoint. At Tutorialspoint provide hundreds of technical tutorials for free.

Contents of the file after replacing the desired word:
Hello how are you welcome to TP. At Tutorialspoint provide hundreds of technical tutorials for free.

Note − All these methods are case sensitive.


Advertisements