三种java 去掉字符串中的重复字符函数
*/
public static void main(string[] args) {
system.out.println(removerepeatedchar("ddddccffgd"));
}public static string removerepeatedchar(string s) {
if (s == null)
return s;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();
}
//实例二java去掉重复字符串private static string removesamestring(string str)
{
set<string> mlinkedset = new linkedhashset<string>();
string[] strarray = str.split(" ");
stringbuffer sb = new stringbuffer();
for (int i = 0; i < strarray.length; i++)
{
if (!mlinkedset.contains(strarray[i]))
{
mlinkedset.add(strarray[i]);
sb.append(strarray[i] + " ");
}
}
system.out.println(mlinkedset);
return sb.tostring().substring(0, sb.tostring().length() - 1);
}
/*
看个实例
string str = "1,2,3,3,2,1"; 去掉重复的部分,得到 str= "1,2,3";
*/string[] array = str.split(,);
arraylist list = new arraylist();
for (int i = 0;i < array.length;i++)
{
if (!list.contains(array[i]))
{
list.add(array[i]);
}
}
return list;