如何用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)
System.out.println(str.replaceAll("is|big|attractive", ""));
Run Code Online (Sandbox Code Playgroud)