C#过滤字符串中的重复字符

    技术2022-07-02  74

    第一种

    public static String[] RemoveDup(string[] myData)    {               if (myData.Length > 0)        {            Array.Sort(myData);//用了数组的排序,个人感觉在数组元素很多的时候能够提高一点效率吧!

                int size = 1; //at least 1             for (int i = 1; i < myData.Length; i++)                if (myData[i] != myData[i - 1])                    size++;

                String[] myTempData = new String[size];

                int j = 0;

                myTempData[j++] = myData[0];

                for (int i = 1; i < myData.Length; i++)                if (myData[i] != myData[i - 1])                    myTempData[j++] = myData[i];

                return myTempData;        }

            return myData;    }

    第二种

        public static String[] RemoveDupstring(string myString)    {        String[] myData = myString.Split('#');        if (myData.Length > 0)        {            //Array.Sort(myData);

                int size = 1; //at least 1             for (int i = 1; i < myData.Length; i++)                if (myData[i] != myData[i - 1])                    size++;

                String[] myTempData = new String[size];

                int j = 0;

                myTempData[j++] = myData[0];

                for (int i = 1; i < myData.Length; i++)                if (myData[i] != myData[i - 1])                    myTempData[j++] = myData[i];

                return myTempData;        }

            return myData;    }

    第三种

            public static string [] CheckDoubleString(string sTemp)        {            char[] sArray = sTemp.ToCharArray();            long lCount = 0;            string sT = "";            StringBuilder sb = new StringBuilder();            for (int i = 0; i < sArray.Length - 1; i++)            {

                    if (sArray[(i + 1) % sArray.Length] != sArray[i % sArray.Length])                    sb.Append(sArray[i]+“|”);

                }            Console.WriteLine(sb.ToString());

               return sb.split['|'];        }


    最新回复(0)