martin_tsv
martin_tsv

Reputation: 39

How to replace specific word in text with asterisks"*" equal to the word's length?

On the first input line i have the words should be replaced. The second input line is for the text.

This is the input:

Linux, Windows

It is not Linux, it is GNU/Linux. Linux is merely the kernel, while GNU adds the functionality. Therefore we owe it to them by calling the OS GNU/Linux! Sincerely, a Windows client

and this should be the output:

It is not *****, it is GNU/*****. ***** is merely the kernel, while GNU adds the functionality. Therefore we owe it to them by calling the OS GNU/*****! Sincerely, a ******* client

This is my code:

String[] words = br.readLine().split(", ");
String text = br.readLine();

for (String word : words) {
    while (text.contains(word)) {    
        text = text.replace(word, "*");// i can replace only the first character in the word with asterisks.
    }
}
System.out.println(text);

Upvotes: 1

Views: 2312

Answers (4)

user330315
user330315

Reputation:

You can create a replacement word by replacing any character with a * in each word:

for (String word : words)
{
  String replacement = word.replaceAll(".", "*");
  text = text.replaceAll(word, replacement);
}

word.replaceAll(".", "*") will replace any character in the word with a *

Upvotes: 0

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59996

You can use String.format to solve your problem, here is an example :

String[] words = {"Linux", "Windows"};//create an array for your word
for(String word : words){//loop throw this array
   str = str.replace(word, String.format("%0" + word.length() + "d", 0).replace("0", "*"));
}
System.out.println(str);

String.format("%0" + word.length() + "d", 0).replace("0", "*") will create a string of * of length word.length so if the word is Linux it will create a String of * of length 5 *****, and so on...

Output

It is not *****, it is GNU/*****. ***** is merely the kernel, while GNU adds 
the functionality. Therefore we owe it to them by calling the 
OS GNU/*****! Sincerely, a ******* client

Upvotes: 0

Henry Maathuis
Henry Maathuis

Reputation: 123

You can do something like this. This could be implemented a lot more efficient, but it suffices for your task. It basically looks for occurances of Windows and Linux and replaces those with the appropriate amount of asterisks given the word length.

String wordLinux = "Linux";
String wordWindows = "Windows";
String s = "It is not Linux, it is GNU/Linux. Linux is merely the kernel, while GNU adds the functionality. Therefore we owe it to them by calling the OS GNU/Linux! Sincerely, a Windows client";

StringBuilder sbLinux = new StringBuilder();
for (int idx = 0; idx != wordLinux.length(); ++idx)
    sbLinux.append("*");
s = s.replaceAll(wordLinux, sbLinux.toString());    

StringBuilder sbWindows = new StringBuilder();
for (int idx = 0; idx != wordWindows.length(); ++idx)
    sbWindows.append("*");
s = s.replaceAll(wordWindows, sbWindows.toString());    

System.out.println(s);

The output of the program yields:

It is not *****, it is GNU/*****. ***** is merely the kernel, while GNU adds the functionality. Therefore we owe it to them by calling the OS GNU/*****! Sincerely, a ******* client

Upvotes: 2

Sweeper
Sweeper

Reputation: 272000

First, you don't need to use a while loop. replace will automatically replace every occurrence.

Second, you can construct a new string with a certain amount of a certain character using a StringBuilder:

StringBuilder sb = new StringBuilder();
for (int i = 0 ; i < someNumber ; i++) {
    sb.append("*");
}

I think this is all you need to figure this out yourself. Good luck!

Upvotes: 0

Related Questions