public class QuChong {
public static void main(String[] arg) {
Qu("abcddedddabc");// 结果是 abcde
}
/**
* 1、定义一个StringBuffer
* 2、s.charAt(i)遍历、比较
* 3、利用indexOf()跟LastindexOf()推断相同字符
*
*
*/
public static void Qu(String s) {
StringBuffer sb = new StringBuffer();
int len = s.length();
int i = 0;
boolean flag = false;
for (i = 0; i < len; i++) {
char c = s.charAt(i);
if (s.indexOf(c) != s.lastIndexOf(c)) {
flag = false;
}else{
flag = true;
}
if(i==s.indexOf(c))
flag=true;
if (flag) {
sb.append(c);
}
}
System.out.print(sb.toString());
}
} |
|