Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace specific word from String

Tags:

java

android

how can I replace a specific word from user input and display it as an asterisk. This is my code.

String[] filteredWords = ["apple", "banana", "orange"];

private String convertWord(String input) {
    StringBuilder asterisk = new StringBuilder();
    for (String filter: filteredWords) {
        if (input.toLowerCase().contains(filter)) {
            for (int i = 0; i < filter.length(); i++) {
                asterisk.append("*");
            }
            input = input.replace(filter, asterisk.toString());
        }
    }

    return input;
}

for example the user input or type "This is apple and not orange", the expected output would be "This is ***** and not ******".

but my problem here is when the user type (with a character casing) "This is Apple and not oRaNGE" or "This is aPpLe and not Orange" the output is not changing. The word apple and orange are not being replace with an asterisk.

any help is appreciated. Thank you!

like image 372
dotGitignore Avatar asked Jun 26 '20 06:06

dotGitignore


People also ask

How do you replace a specific word in a string in Python?

Python String replace() MethodThe replace() method replaces a specified phrase with another specified phrase. Note: All occurrences of the specified phrase will be replaced, if nothing else is specified.


2 Answers

I like using regex replacement here, with an alternation containing all your search terms:

String[] filteredWords = { "apple", "banana", "orange" };
String regex = "(?i)\\b(?:" + String.join("|", filteredWords) + ")\\b";

private String convertWord(String input) {
    input = input.replaceAll(regex, "*");
    return input;
}

convertWord("Apple watermelon ORANGE boot book pear banana");

The output from the above method call is:

* watermelon * boot book pear *
like image 128
Tim Biegeleisen Avatar answered Oct 12 '22 01:10

Tim Biegeleisen


String[] filteredWords = ["apple", "banana", "orange"];

private static String convertWord(String input) {
        StringBuilder asterisk = new StringBuilder();
        String inputInLower = input.toLowerCase();
        for (String filter: filteredWords) {
                if (inputInLower.contains(filter)) {
                    for (int i = 0; i < filter.length(); i++) {
                        asterisk.append("*");
                    }
                    inputInLower = inputInLower.replace(filter, asterisk.toString());
                    asterisk.setLength(0);
                }
            }
            return inputInLower;
        }

There was 2 problems I found in your code

  1. Your are using the input string which is not converted to lower characters, so array value is not matching. You can solve this by using a new variable as I use in answer or convert to lower before replacing.
  2. Second problem was with StringBuilder you have to clear builder after every iteration or else last characters will be append in that.
like image 45
A y Avatar answered Oct 12 '22 03:10

A y