java去除重複字串
阿新 • • 發佈:2019-02-16
package org.yangfan.去掉重複string; public class UnRepairString { public static void main(String[] args) { System.out.println(removerepeatedchar("dhh45iuhhlgg25444lhhdddccffgd")); } public static String removerepeatedchar(String s) { if (s == null) return s; StringBuffer sb = new StringBuffer(); 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+1取下一個元素 i++; } } return sb.toString(); } }