
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- What is the difference between replace() and replaceAll() in Java?
- Replace() vs ReplaceAll() in Golang
- Java String Methods
- Java String toUpperCase() and toLowerCase() methods
- Matcher replaceFirst() method in Java with Examples
- Java String Comparison Methods.
- Java String replace() method example.
- Replace String with another in java.
- Matcher replaceAll() method in Java with Examples
- Replace Character in a String in Java without using replace() method
- How to use Java string replace method?
- Replace one string with another string with Java Regular Expressions
- Java methods to convert Double to String
- How to replace Digits into String using Java?
- How to replace characters on String in Java?
