日期:2014-05-17  浏览次数:20951 次

一个字符串中去掉相同的字符
如:
String str=“2,3,4,3,2,5”;
咋样去掉这里相同的2和3
就是最终要的结果是:
String s=“2,3,4,5”;
字符串,字符

------解决方案--------------------

  String str="2,3,4,3,2,5";
            string[]  list = str.Split(',');
            string ret = string.Empty;

            ArrayList list1 = new ArrayList();
            for (int i = 0; i < list.Length; i++)
            {
                if (!list1.Contains(list[i]))
                {
                    list1.Add(list[i]);
                }
            }

            for (int i = 0; i < list1.Count; i++)
            {
                ret += list1[i] + ",";
            }
            Console.WriteLine(ret);
            Console.Read();

------解决方案--------------------

 string str = "2,3,5,3,6,7,6,8,5,4,3,2,1,89,54,3,2,45,12,345345,123123,34,3,3,2,";
            string[] stringArray  = str.Split(',');
            List<string> listString = new List<string>();
            foreach (string eachString in stringArray)
            {
                if (!listString.Contains(eachString))

                    listString.Add(eachString);

            }
            Console.WriteLine(string.Join(",", listString.ToArray()));
            Console.Read();

------解决方案--------------------
string str = "2,3,5,3,6,7,6,8,5,4,3,2,1,89,54,3,2,45,12,345345,123123,34,3,3,2,";
            string[] stringArray = str.Split(',').Distinct().ToArray();
......
------解决方案--------------------