Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我应该使用remmove吗?这在java中正确吗?_Java - Fatal编程技术网

我应该使用remmove吗?这在java中正确吗?

我应该使用remmove吗?这在java中正确吗?,java,Java,在练习中,他们告诉我们如果字符串包含00或0,请删除它 String rse="12+00+9+88"; String re="[0]{1,2}\\+"; String oper[]=chaine.split("[0-9]{1,2}\\+"); for(int i=1;i<oper.length;i++) { if (oper.equals(re)) { } } String rse=“12+00+9+88”;

在练习中,他们告诉我们如果字符串包含00或0,请删除它

String rse="12+00+9+88";

String re="[0]{1,2}\\+";
String oper[]=chaine.split("[0-9]{1,2}\\+");
for(int i=1;i<oper.length;i++) {
    if (oper.equals(re)) {
    }
}
String rse=“12+00+9+88”;
字符串re=“[0]{1,2}\\\+”;
字符串oper[]=chaine.split(“[0-9]{1,2}\\\+”);

对于(inti=1;i您可以使用
String.replaceAll
并使用2个正则表达式

  • (0+)\+
    删除零,后跟
    +
  • \+(0+
    ,特定情况下,当零是最后一个时,
    +
    在其前面
List values=Arrays.asList(
"12+00+9+88",
"12+0+9+88",
"00+9+88",
"0+9+88",
"12+00",
"12+0"
);
用于(字符串s:值){
字符串r=s.replaceAll(“(0+\\+”,”).replaceAll(“\\+(0+”,”);
系统输出println(r);
}
12+9+88
12+9+88
9+88
9+88
12
12

如果字符串包含00,您只需使用contains方法检查即可。然后,您可以使用string的replaceAll()方法将零替换为空字符串“”,非常感谢
List<String> values = Arrays.asList(
    "12+00+9+88",
    "12+0+9+88",
    "00+9+88",
    "0+9+88",
    "12+00",
    "12+0"
);

for (String s : values) {
    String r = s.replaceAll("(0+)\\+", "").replaceAll("\\+(0+)", "");
    System.out.println(r);
}

12+9+88
12+9+88
9+88
9+88
12
12