/**
* 字符串去重
*
* @param str 待去重字符串 ABBACCDDEF
* @return 去重后字符串 ABCDEF
*/
public String removeDuplicate(String str) {
if (str == null)
return str;
int len = str.length();
if (len < 2)
return str;
char[] chars = str.toCharArray();
int i, j;
int index = 0;
for (i = 0; i < len; ++i) {
if (chars[i] != '\0') {
chars[index++] = chars[i];
for (j = i + 1; j < len; ++j) {
if (chars[j] == chars[i])
chars[j] = '\0';
}
}
}
return new String(chars);
}
/**
* 删除相同字符连续出现
*
* @param s hello
* @return helo
*/
public static String uniqNearString(String s) {
if (s == null)
return "";
StringBuilder sb = new StringBuilder();
int i = 0, len = s.length();
while (i < len) {
char c = s.charAt(i);
sb.append(c);
i++;
while (i < len && s.charAt(i) == c) {
i++;
}
}
return sb.toString();
}
String字符串去重,保证顺序
最新推荐文章于 2024-10-16 22:47:20 发布