本帖最后由 satdown 于 2012-06-08 16:37:50 编辑

解决方案 »

  1.   


    string s = "xxxxxx,xxxxxx,xxxxxxxx。xxxxxxxx。xxxxxxxxx;xxxxx!xxxxxx!";MessageBox.Show(s.IndexOf(',').ToString());
    MessageBox.Show(s.LastIndexOf(',').ToString());
    ……
      

  2.   


    string s = "xxxxxx,xxxxxx,xxxxxxxx。xxxxxxxx。xxxxxxxxx;xxxxx!xxxxxx!";
    List<int> position = new List<int>();
    int index = -1;
    while ((index = s.IndexOf(',', index + 1)) >= 0)
    {
        position.Add(index);
    }
      

  3.   

    如需加标点符号,请在pattern正则表达式中的中括号内 string tempStr = "xxxxxx,xxxxxxxx,xxxxxxxx。xxxxxxxx。xxxxxxxxx;xxxxx!xxxxxx!";
                    string pattern = @"[,。!]";                var data = Regex.Matches(tempStr, pattern).Cast<Match>().Select((a ,i)=> new { desc=a.Value,index=a.Index});
                    /*
                     * + [0] { desc = ",", index = 6 } <Anonymous Type>
                       + [1] { desc = ",", index = 15 } <Anonymous Type>
                       + [2] { desc = "。", index = 24 } <Anonymous Type>
                       + [3] { desc = "。", index = 33 } <Anonymous Type>
                       + [4] { desc = "!", index = 49 } <Anonymous Type>
                       + [5] { desc = "!", index = 56 } <Anonymous Type>                 */
      

  4.   

     class Program
        {
            static void Main(string[] args)
            {
                string s = "xxxxx,xxxxxxx,xxxxxxx.xxxxxxx.xxxxxxxx;xxxxx!xxxxx!";
                
                for (int i = 0; i < s.Length; i++)
                {
                    switch (s[i])
                    {
                        case ',':
                            Console.WriteLine("The position of ',' is {0}: ", i);
                            break;
                        case '.':
                            Console.WriteLine("The position of '.' is {0}: ", i);
                            break;
                        case '!':
                            Console.WriteLine("The position of '!' is {0}: ", i);
                            break;
                        default: break;
                    }
                }
            }
        }
      

  5.   


    while循环里面从index+1位置开始找下一个','的位置,初始的时候肯定得从0开始,所以index的初始值要赋为-1