替换java字符串中未知数量的值

Mod*_*est 0 java string

如何用java中的字符串中的空字符串""替换多个单词.我尝试使用for循环来替换单引号中的那些,然后在下面的数组中添加,但它会替换每个打印输出一个单词.

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 );
}
Run Code Online (Sandbox Code Playgroud)

我得到这个输出:

> this house ' ' really 'big' and 'attractive'.
> this house 'is' really ' ' and 'attractive'.
> this house 'is' really 'big' and ' '.
Run Code Online (Sandbox Code Playgroud)

我需要的是这个:

> this house ' ' really ' ' and ' '.
Run Code Online (Sandbox Code Playgroud)

Rei*_*eus 6

System.out.println(str.replaceAll("is|big|attractive", ""));
Run Code Online (Sandbox Code Playgroud)

  • `String.join("|",values)`可以帮助从数组中获取正则表达式. (3认同)