使用Set去除String中的重复字符
使用Set去除String中的重复字符
public class test14 { public static void main(String[] args) { String str = "ababc"; System.out.println(removeChar(str)); } public static String removeChar(String str){ System.err.println(str + ",长度为:" + str.length()); Set<Character> set = new HashSet<Character>(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < str.length(); i++) { set.add(str.charAt(i)); } for (Character c : set) { sb.append(c); } return sb.toString(); } }
yian