java 中StringBuffer 去重复值
2个回答
展开全部
String x = "abc45345dfsadfdfhgfhda";
StringBuffer strB = new StringBuffer(x);
ArrayList al = new ArrayList();
for (int i = 0; i < strB.length(); i++) {
for (int j = 0; j < strB.length(); j++) {
if (strB.charAt(i) == strB.charAt(j) && i != j) {
al.add(strB.charAt(i));
strB.deleteCharAt(i);
strB.deleteCharAt(j - 1);
}
}
}
System.out.println("原字符串为:" + x + "\n新字符串为:" + strB);
System.out.print("重复的字符为:");
for (int i = 0; i < al.size(); i++) {
System.out.print(al.get(i) + ",");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询