将字符串中重复的字符去除

String str = "huechsghkshhj";
int len = str.length();
char[] c = new char[len];
int i = 0;
for (int index = 0; index < len; index++) {
    boolean flag = false;
    char ch = str.charAt(index);
    for (char c1 : c) {
        if (c1 == ch) {
            flag = true;
            break;
        }
    }
    //每次c数组的元素值会改变,因此需要得到c遍历完之后的结果再来进行赋值
    if (!flag) {
        c[i++] = ch;
    }
}
str = new String(c);
System.out.println(str);
posted @ 2022-11-09 21:58  Liku007  阅读(21)  评论(0编辑  收藏  举报