public class Test{
public static void main(String[] args) {
String str = "aabbbccccdddddeeeeeeeeefff234tttdddfffbbbggg";
String result = removeRepeatChar(str);
System.out.println("去重前----:" + str);
System.out.println("去重后----:" + result);
}
public static String removeRepeatChar(String s) {
StringBuffer sb = new StringBuffer();
int i = 0;
while (i < s.length()) {
char c = s.charAt(i);
sb.append(c);
while (i < s.length() && s.charAt(i) == c) {// 这个是如果这两个值相等,就让i+1取下一个元素
i++;
}
}
return sb.toString();
}
}
去除字符串中相邻重复的字符
猜你喜欢
转载自blog.csdn.net/qq_28061489/article/details/85163124
今日推荐
周排行