String字符串去除重复值

例: String a="06-104,07-104,06-104,07-104,03-103,04-103,03-103,04-103,05-102,";

去掉重复的,然后打印出来是

06-104,07-104,03-103,04-103,05-102

public  static  void  main(String[] args) {
String a= "06-104,07-104,06-104,07-104,03-103,04-103,03-103,04-103,05-102," ;
a = a.replaceAll( "^,*|,*$" , "," );
String reg =  "(,[^,]+,).*\\1+" ;
while (a.matches( "^.*"  + reg +  ".*$" )){
     a = a.replaceAll(reg, "$1" );
}
a = a.replaceAll( "^,+|,+$" , "" );
System.out.println(a);
}

我自己的:

private String  replaceAll(String str){
str = str.replaceAll("^,*|,*$",",");
String reg = "(,[^,]+,).*\\1+";
while(str.matches("^.*" + reg + ".*$")){
str = str.replaceAll(reg,"$1");
}
str= str.replaceAll("^,+|,+$","");
return str;
}

猜你喜欢

转载自blog.csdn.net/zezezuiaiya/article/details/80365175