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('"'));
}
}
}
}