- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Program to replace all the characters in of a file with '#' except a particular word in Java
The split() method of the String class. splits the current string around matches of the given regular expression. The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string.
The replaceAll() method of the String class accepts two strings representing a regular expression and a replacement String and replaces the matched values with given String.
To replace all the characters in of a file with '#' except a particular word (one way) −
Read the contents of a file to a String.
Create an empty StringBuffer object.
Split the obtained string int o array of String using the split() method.
Traverse through the obtained array.
If any element in it matches to the required word, append it to the String buffer.
Replace the characters in all the remaining words with ‘#’ and append them to the StringBuffer object.
Finally convert the StingBuffer to String.
Example
Assume we have a file named sample.txt with the following contents −
Hello how are you welcome to Tutorialspoint we provide hundreds of technical tutorials for free.
Following program reads the contents of a file to a string replaces all the characters in it with '#' except a particular word.
import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; public class ReplaceExcept { public static String fileToString() 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); } return sb.toString(); } public static void main(String args[]) throws FileNotFoundException { String contents = fileToString(); System.out.println("Contents of the file: \n"+contents); //Splitting the words String strArray[] = contents.split(" "); System.out.println(Arrays.toString(strArray)); StringBuffer buffer = new StringBuffer(); String word = "Tutorialspoint"; for(int i = 0; i < strArray.length; i++) { if(strArray[i].equals(word)) { buffer.append(strArray[i]+" "); } else { buffer.append(strArray[i].replaceAll(".", "#")); } } String result = buffer.toString(); System.out.println(result); } }
Output
Contents of the file: Hello how are you welcome to Tutorialspoint we provide hundreds of technical tutorials for free. [Hello, how, are, you, welcome, to, Tutorialspoint, we, provide, hundreds, of, technical, tutorials, for, free.] #######################Tutorialspoint ############################################
- Related Articles
- Program to replace all the characters in of a file with '#' except a particular word in Java
- Python program to replace all Characters of a List except the given character
- Python Program to Replace all words except the given word
- How to replace all occurrences of a word in a string with another word in java?
- Java Program to replace a word with asterisks in a sentence
- How to replace characters except last with a mask character in JavaScript?
- Replace all characters in a string except the ones that exist in an array JavaScript
- C++ Program to Remove all Characters in a String Except Alphabets
- Program to replace all digits with characters using Python
- Python Program to replace a word with asterisks in a sentence
- Reverse word starting with particular characters - JavaScript
- In MySQL how to replace all NULL values in a particular field of a particular table?
- Java program to count the characters in each word in a given sentence
- PHP program to replace a word with a different symbol in a sentence
- How to grep and replace a word in a file on Linux?
- Python Program to Replace all Occurrences of ‘a’ with $ in a String
