小编Mod*_*est的帖子

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

如何用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)

java string

0
推荐指数
1
解决办法
90
查看次数

标签 统计

java ×1

string ×1