0

How do i replace multiple words with an empty string "" in a string with java. I tried with for loop to replace those in single quotes by adding then in an array like below, but it replaces one word per printout.

String str = "this house 'is' really 'big' and 'attractive'.";
String[] values={"is","big","attractive"};
for(String s: values){
    String replaced = str.replace(s, "");
    System.out.println( replaced );
}

I'm getting this output:

> this house ' ' really 'big' and 'attractive'.
> this house 'is' really ' ' and 'attractive'.
> this house 'is' really 'big' and ' '.

What I need is this:

> this house ' ' really ' ' and ' '.
1
  • Remius answer is correct, but if you want to make your code working replace "String replaced" with "str" (and thereby improve the result at every iteration) and move the println behind the for loop. Commented Mar 15, 2018 at 12:35

4 Answers 4

6
System.out.println(str.replaceAll("is|big|attractive", ""));
Sign up to request clarification or add additional context in comments.

1 Comment

String.join("|", values) could help to get the regex from the array.
1

Why your approach is wrong

Strings in Java are immutable - when you call replace , it doesn't change the contents of the existing string - it returns a new string with the modifications. So you want:

str= str.replace(s, ""); instead of String replaced = str.replace(s, "");

Also, write this code after the for loop: System.out.println(str); and omit System.out.println( replaced ); as its placement is inside the for loop right now resulting into multiple statements getting printed.

Note that after doing all this, code would print th house '' really '' and ''. and NOT the desired output.

Why follow Reimeus's answer OR use a generic regex expression

Even after correcting your code, you would NOT get the desired results, hence :)

Comments

1

You re-start each iteration from the original String, which you don't modify.

You could change the String your str refers to and use that, like this:

      for(String s: values){
             str = str.replace(s, "");
             System.out.println( str );
       }

Comments

0

You are re-initializingreplaced in every iteration. So you end up with a string in which only the last element of array values is replaced. To get the desired output, try this:

String replaced =  str;
for(String s: values){
    replaced = replaced.replace(s, "");
}

7 Comments

This answer makes no sense at all. String replaced = s; will cause a Compilation error
Rookie mistake. Corrected! :)
so why do you need the 'replaced' variable at all? just replacing replaced by str would do the trick ;)
OP used a seperate variable for output. So I did the same. (This is my first ever answer on SO. Lot to learn :D )
But even this approach won't produce the desired output.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.