public String do(String str,int i){
//实现方法
return null;
} 如:str="12,23,33,43,54,6,34,233,455"; //其中里面都是唯一的整数
如果 i = 12 方法do 返回 "23,33,43,54,6,34,233,455"
如果 i = 33 方法do 返回 "12,23,43,54,6,34,233,455"
如果 i = 455 方法do 返回 "12,23,33,43,54,6,34,233" 如果i 为不在字符串中的数字,那么返回这个字符串
//实现方法
return null;
} 如:str="12,23,33,43,54,6,34,233,455"; //其中里面都是唯一的整数
如果 i = 12 方法do 返回 "23,33,43,54,6,34,233,455"
如果 i = 33 方法do 返回 "12,23,43,54,6,34,233,455"
如果 i = 455 方法do 返回 "12,23,33,43,54,6,34,233" 如果i 为不在字符串中的数字,那么返回这个字符串
然后逐个比较一下
public String do(String str,int i){
//实现方法
String s = "";
String[] arr = str.split("[ ]*,[ ]*");
for(int i = 0;i < arr.length;i++){
if(Integer.parseInt(arr[i]) != i){
s += arr[i] + ",";
}
}
return s;
} 这个代码应该能满足你吧?
public static void main(String[] args) {
String str="12,23,33,43,54,6,34,233,455";
System.out.println(dosth(str,12));
System.out.println(dosth(str,23));
System.out.println(dosth(str,43));
System.out.println(dosth(str,34));
System.out.println(dosth(str,455));
}
public static String dosth(String str,int i)
{
return str.replace(i+"", "").replaceAll(",$|^,", "");//第二次替换主要是为了去掉开头或结果多出来的逗号
}
public String do(String str,int i){
//实现方法
String s = "";
String[] arr = str.split("[ ]*,[ ]*");
for(int j = 0;j < arr.length;j++){
if(Integer.parseInt(arr[j]) != i){
s += arr[j] + ",";
}
}
return s;
} 上面的代码for循环中有点出入,这样应该没问题的。可以试一下的
String str="12,23,33,43,54,6,34,233,455";
System.out.println(dosth(str,12));
System.out.println(dosth(str,23));
System.out.println(dosth(str,43));
System.out.println(dosth(str,34));
System.out.println(dosth(str,455));
}
public static String dosth(String str,int i)
{
return str.replaceAll(i+",|,"+i+"$", "");
} 23,33,43,54,6,34,233,455
12,33,43,54,6,34,233,455
12,23,33,54,6,34,233,455
12,23,33,43,54,6,233,455
12,23,33,43,54,6,34,233