package test;
public class StringHeBing {
public static String merge(String str1,String str2){
int len = str2.length();
for (int i = 0; i < len; i++) {
char c = str2.charAt(i);
if (str1.indexOf(c) == -1) {
str1 = str1.concat(c+"");
}
}
System.err.println(str1);
return str1;
}
public static void main(String[] args) {
String str1 = "上海金融大厦";
String str2 = "上港集团";
merge(str1, str2);
}
}
上面的做法只把加加进来的字符串去重,而未把原来的字符串去重复,下面做了个新的
public static String merge1(String str1,String str2){
String add = str1.concat(str2);
String result= add.charAt(0)+"";
for (int i = 1; i < add.length(); i++){
char c = add.charAt(i);
if (result.indexOf(c) == -1) {
result = result.concat(c+"");
}
}
return result;
}
public class test1 {
public static void main(String args[]) {
String str1 = "abdasidan";
String str2 = "absdaddsa";
String str = str1 + str2;//合并
System.out.println(str);
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
for (int j = i+1;jchar cc = str.charAt(j);
if (c == cc) {
str = str.substring(0, j) + str.substring(j + 1);
j--;
}
}
}
System.out.println(str);
}
}
其实很简单的,用正则一句就够了,没必要写那么多
String a = "aabceesiojkdd";
String b= "khiehkkkhhssdfesee";
String c = (a+b).replaceAll("(.)\\1+", "");
System.out.println(c);//这里c就没有重复的字符了