比如 String a = "1,2,3,4,5,6,7,8,9"
我要移除某个值,比如是5,那移除后的字符串应该是"1,2,3,4,6,7,8,9"
移除的值是动态的,请问该怎么做......请个位高手帮忙1
谢谢个位,我自己已经解决了
String informationid = "";
String arr[]= memberService.getMemberByMembernick(member.getMembernick()).getInformation().split(",");
for(int i = 0;i<arr.length;i++){
if(!(arr[i].equals(messageid.toString()))){
informationid+=arr[i]+",";
}
}
System.out.println(informationid.substring(0,informationid.length()-1));
楼上的明显不行啊,这不还是只能去掉一个数字么?如果一个数组里有好几个相同的怎么办?
需要import java.util.Scanner;
楼主可根据需要自己改为传参函数
public static void main(String args[]){
System.out.println("请输入数字组,数字请以,隔开");
Scanner scanner =new Scanner(System.in);
String str=null;
str=scanner.nextLine();
System.out.println("请输入要去除的数");
String rm=null;
rm=scanner.nextLine();
if(str.indexOf(rm)==-1){
System.out.println("要去除的数字不存在");return;}
System.out.println("要去除第几个(输入个数)?还是都除去?(输入’ -1‘)");
int n=scanner.nextInt();
if(n==0||n<-1){
System.out.println("非法输入");return;}
else if(n==-1){
str=","+str;
rm=","+rm;
str=str.replaceAll(rm,"");
if(str.charAt(0)==',')
str=str.substring(1);
System.out.println("结果为:"+str);
return; }
String str2[]=str.split(",");
for(int k=0;k<str2.length;k++)
if(str2[k].equals(rm)){
n--;
if(n==0){
n=-k;break;
} }
if(n>0){
System.out.println("要去除的数字不存在");return;}
String str3="";
boolean l=false;
for(int k=0;k<str2.length;k++){
if(k==-n)
continue;
if(l)
str3+=",";
str3+=""+str2[k];
l=true;
}
System.out.println("结果为:"+str3); }
用例:
请输入数字组,数字请以,隔开
3,999,324,4,4,3,6,3,3,3,536,4,58
请输入要去除的数
3
要去除第几个(输入个数)?还是都除去?(输入’ -1‘)
1
结果为:999,324,4,4,3,6,3,3,3,536,4,58
请输入数字组,数字请以,隔开
3,999,324,4,4,3,6,3,3,3,536,4,58
请输入要去除的数
3
要去除第几个(输入个数)?还是都除去?(输入’ -1‘)
4
结果为:3,999,324,4,4,3,6,3,3,536,4,58
需要import java.util.Scanner;
楼主可根据需要自己改为传参函数
public static void main(String args[]){
System.out.println("请输入数字组,数字请以,隔开");
Scanner scanner =new Scanner(System.in);
String str=null;
str=scanner.nextLine();
System.out.println("请输入要去除的数");
String rm=null;
rm=scanner.nextLine();
if(str.indexOf(rm)==-1){
System.out.println("要去除的数字不存在");return;}
System.out.println("要去除第几个(输入个数)?还是都除去?(输入’ -1‘)");
int n=scanner.nextInt();
if(n==0||n<-1){
System.out.println("非法输入");return;}
else if(n==-1){
str=","+str;
rm=","+rm;
str=str.replaceAll(rm,"");
if(str.charAt(0)==',')
str=str.substring(1);
System.out.println("结果为:"+str);
return; }
String str2[]=str.split(",");
for(int k=0;k<str2.length;k++)
if(str2[k].equals(rm)){
n--;
if(n==0){
n=-k;break;
} }
if(n>0){
System.out.println("要去除的数字不存在");return;}
String str3="";
boolean l=false;
for(int k=0;k<str2.length;k++){
if(k==-n)
continue;
if(l)
str3+=",";
str3+=""+str2[k];
l=true;
}
System.out.println("结果为:"+str3); }
用例:
请输入数字组,数字请以,隔开
3,999,324,4,4,3,6,3,3,3,536,4,58
请输入要去除的数
3
要去除第几个(输入个数)?还是都除去?(输入’ -1‘)
1
结果为:999,324,4,4,3,6,3,3,3,536,4,58
请输入数字组,数字请以,隔开
3,999,324,4,4,3,6,3,3,3,536,4,58
请输入要去除的数
3
要去除第几个(输入个数)?还是都除去?(输入’ -1‘)
4
结果为:3,999,324,4,4,3,6,3,3,536,4,58
温馨提示:答案为网友推荐,仅供参考
第1个回答 推荐于2016-01-23
这样看可以吧,其中a是指定字符串,keynum是要查询的
public String selectStr(String a, String keynum) {
String[] aStrings = a.split(",");
StringBuffer sBuffer = new StringBuffer();
if (a.indexOf(keynum) != -1 && aStrings.length > 0) {
for (int i = 0; i < aStrings.length; i++) {
if (aStrings[i].equals(keynum)) {
sBuffer.append(aStrings[i]);
}
}
}
return sBuffer.toString();
}
public static void main(String[] args) {
String a = "1,2,3,4,5,6,7,8,9";
String string = selectStr(a, "5");
System.out.println(string);
}本回答被提问者采纳
public String selectStr(String a, String keynum) {
String[] aStrings = a.split(",");
StringBuffer sBuffer = new StringBuffer();
if (a.indexOf(keynum) != -1 && aStrings.length > 0) {
for (int i = 0; i < aStrings.length; i++) {
if (aStrings[i].equals(keynum)) {
sBuffer.append(aStrings[i]);
}
}
}
return sBuffer.toString();
}
public static void main(String[] args) {
String a = "1,2,3,4,5,6,7,8,9";
String string = selectStr(a, "5");
System.out.println(string);
}本回答被提问者采纳
第2个回答 2011-08-29
楼上的方法是不行的,如某个元素为51,是会出错的
如果不用到集合,最好是使这个字符串先变成有规则的字符串:
String oa = "1,2,3,4,5,6,71,18,9";
String a = "," + oa + ",";
String na = a.replace("," + "18" + ",", ",");
System.out.println(na.substring(1, na.length() - 1));
为了节省内存,可以用StringBuffer代替string的拼接,然后封装成一个方法,传入的是原始字符串和需要移除的元素,返回移除后的字符串
封装后的方法:
/**
* 操作字符串
* @param sb 原字符串
* @param param 要移除的字段
* @return
*/
public String func(String sb,String param){
StringBuffer nsb = new StringBuffer();
String a = nsb.append(",").append(sb).append(",").toString();
StringBuffer np = new StringBuffer();
np.append(",").append(param).append(",");
String na = a.replace(np.toString(), ",");
return na.substring(1, na.length() - 1);
}
如果不用到集合,最好是使这个字符串先变成有规则的字符串:
String oa = "1,2,3,4,5,6,71,18,9";
String a = "," + oa + ",";
String na = a.replace("," + "18" + ",", ",");
System.out.println(na.substring(1, na.length() - 1));
为了节省内存,可以用StringBuffer代替string的拼接,然后封装成一个方法,传入的是原始字符串和需要移除的元素,返回移除后的字符串
封装后的方法:
/**
* 操作字符串
* @param sb 原字符串
* @param param 要移除的字段
* @return
*/
public String func(String sb,String param){
StringBuffer nsb = new StringBuffer();
String a = nsb.append(",").append(sb).append(",").toString();
StringBuffer np = new StringBuffer();
np.append(",").append(param).append(",");
String na = a.replace(np.toString(), ",");
return na.substring(1, na.length() - 1);
}
第3个回答 2011-08-29
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
String a = "1,2,3,4,5,6,7,8,9";
String str =null;
//动态输入
Scanner scan =new Scanner(System.in);
System.out.println("请输入删除数字 (1-9):");
int num = scan.nextInt();
//做个简单判断即可
if (num==1)
{
str = a.replace("1,", "");
}
else
{
String reg=num+"";
str = a.replace(","+reg, "");
}
System.out.println(str);
}
}
public class Test {
public static void main(String[] args) {
String a = "1,2,3,4,5,6,7,8,9";
String str =null;
//动态输入
Scanner scan =new Scanner(System.in);
System.out.println("请输入删除数字 (1-9):");
int num = scan.nextInt();
//做个简单判断即可
if (num==1)
{
str = a.replace("1,", "");
}
else
{
String reg=num+"";
str = a.replace(","+reg, "");
}
System.out.println(str);
}
}
第4个回答 2011-08-29
怎么个动态?
String s="";
s=//此出接收你要删除的值
a=a.replace(s,a);//这个方法怎么用忘了,你查API吧
String s="";
s=//此出接收你要删除的值
a=a.replace(s,a);//这个方法怎么用忘了,你查API吧