- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to replace all occurrences of a word in a string with another word in java?
The replaceAll() method of the String class accepts two strings, One representing a regular expression to find a string and the other representing a replacement string.
And, replaces all the matched sequences with the given String. Therefore, to replace a particular word with another in a String −
Get the required String.
Invoke the replace all method on the obtained string by passing, a regular expression representing the word to be replaced (within word boundaries “\b”) and a replacement string as parameters.
Retrieve the result and print it.
Example
import java.io.File; import java.io.IOException; import java.util.Scanner; public class ReplacingWords { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(new File("D://sample.txt")); String input; StringBuffer sb = new StringBuffer(); while (sc.hasNextLine()) { input = sc.nextLine(); sb.append("
"+input); } String contents = sb.toString(); System.out.println("Contents of the string: "+contents); contents = contents.replaceAll("\bTutorials point\b", "TP"); System.out.println("Contents of the string after replacement: "); System.out.println(contents); } }
Output
Contents of the string: Tutorials point originated from the idea that there exists a class of readers who respond better to on-line content and prefer to learn new skills at their own pace from the comforts of their drawing rooms. At Tutorials point we provide high quality learning-aids for free of cost. Tutorials point recently developed an app to help students from 6th to 12th. Contents of the string after replacement: TP originated from the idea that there exists a class of readers who respond better to on-line content and prefer to learn new skills at their own pace from the comforts of their drawing rooms. At TP we provide high quality learning-aids for free of cost. TP recently developed an app to help students from 6th to 12th.
- Related Articles
- How to replace all occurrences of a string with another string in Python?
- Java program to count occurrences of a word in string
- C program to Replace a word in a text by another given word
- C# program to count occurrences of a word in string
- Python program to count occurrences of a word in a string
- Find all occurrences of a word in array in JavaScript
- How to replace all occurrences of a string in JavaScript?
- Python Program to Replace all Occurrences of ‘a’ with $ in a String
- How to find a replace a word in a string in C#?
- Java Program to replace a word with asterisks in a sentence
- Write a python program to count occurrences of a word in string?
- How can we replace all the occurrences of a substring with another substring within a string in MySQL?
- Java Program to replace all occurrences of a given character in a string
- Program to replace all the characters in of a file with '#' except a particular word in Java
- Replace All Occurrences of a Python Substring with a New String?

Advertisements