最快的去重方式
String s = "J1003、J1004、J1004、J1006";
String str[]=s.split("、");
List list=new ArrayList();
Map map=new HashMap();
for (int i = 0; i < str.length; i++) {
if(map.get(str[i].trim()) ==null){
map.put(str[i].trim(), str[i].trim());
list.add(str[i].trim());
}
}
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i)+"、");
}
使用java中String类自带的正则表达式功能,可以根据、来切分这个字符串变为一个字符串组,然后再两两比较去重后组装就行了
用set,不但重复的去掉了,连序都排好了
String[] arr = s.split("、");
Set set = new TreeSet();
for (int i = 0; i < arr.length; i++) {
set.add(arr[i]);
}
System.out.println(set);
import java.util.*;
public class removechong{
public static void main()
{
String s="J1003、J1004、J1004、J1006";
String array[]=s.split(". ");
HashMap
int index=0;
for(;index
hs.add(array[index],array[index]);
}
Iterator iterator = hashmap.keySet().iterator();
while (iterator.hasNext()) {
System.out.print((String)iterator.next());
}
}
}
Strng s1[]=new String[3];
String s2[]=s.split(",");
for(int i=0;i<3;i++){
if(i!=2){
s1[]=s2[]+",";
}else{
s1[]=s2[];
}
System.out.println(Arrays.toString(s1));
}