link如何实现构造状态机,扫描文本,根据状态决定是否捕获,遇到终止然后切分

link如何实现构造状态机,扫描文本,根据状态决定是否捕获,遇到终止然后切分

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        enum State
        { 
            normal,
            inquote,
        }

        static void Main(string[] args)
        {
            string s = "1,2,3,\"4,5\",6,\"7\"\"8\"";
            List<string> result = new List<string>();
            State state = State.normal;
            string curr = "";
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == '"')
                {
                    if (state == State.normal)
                    {
                        state = State.inquote;
                    }
                    else
                    {
                        if (i == s.Length - 1 || s[i + 1] != '"')
                            state = State.normal;
                    }
                }
                if (s[i] == ',' && state == State.normal)
                {
                    result.Add(curr);
                    curr = "";
                }
                else
                {
                    curr += s[i];
                }
            }
            result.Add(curr);
            foreach (string x in result)
            {
                Console.WriteLine(x.Trim('"'));
            }
        }
    }
}

用正则就够了吧 http://ask.csdn.net/questions/162664

1
2
3
4,5
6
7""8
Press any key to continue . . .