在一个字符串中出现多次的字符怎样让它只出现一次啊  ???比如 字符串:“大家好大家”
让“大家”只出现一次

解决方案 »

  1.   


    string temp = "大家好大家";
                string tempstr = "";
                for (int i = 0; i <= temp.Length - 1; i++)
                {
                    tempstr = temp.Substring(i, 1);
                    if (temp.IndexOf(tempstr) != i)
                    {
                        temp = temp.Remove(i, 1);
                        i = 0;
                    }
                }
                Console.WriteLine(temp);
      

  2.   

        foreach (string i in arrInt)
                {
                    if (arr.IndexOf(i) < 0)
                    {
                        arr.Add(i);
                       
                    }
                }
      

  3.   

    用splid方法拆一下,然后拼起来,出现一次的那个字符串你想放哪都可以~
      

  4.   

    显然我只要回复必然是给出个正则的解决方法。没悬念的写下如此代码,有点累赘的写法,是为了2005通用兼容,避免太Fashion的写法,用低版本编译器的无法编译:
    string result = Regex.Replace(yourStr, Regex.Escape(key), (MatchEvaluator)delegate(Match m)
    {
        if (!first)
        {
            return "";
        }
        else
        {
            first = false;
            return m.Value;
        }
    });
      

  5.   

    贴完整吧。这半截子…        private static void TestRegex30()
            {
                string key = "大家";//指定只能出现一次的字符串
                string yourStr = "大家好大家";
                bool first = true;
                //2005写法
                string result = Regex.Replace(yourStr, Regex.Escape(key), (MatchEvaluator)delegate(Match m)
                {
                    if (!first)
                    {
                        return "";
                    }
                    else
                    {
                        first = false;
                        return m.Value;
                    }
                });            
                Console.WriteLine(result);
            }