IMO the easiest way is to first split your string with ',' and then combine the strings with the characters '(' and ')' together:
string value = @"11,Standard(db=S,api=Standard),UI,1(db=1,api=STANDARD),Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36,1010,9999,1000,9998.0,,1"; List<string> list = new List<string>(); string[] temp = value.Split(','); for (var i = 0; i < value.Split(',').Length; i++) { if (temp[i].Contains('(')) continue; if (temp[i].Contains(')')) { list.Add(temp[i - 1] + temp[i]); continue; } list.Add(temp[i]); } foreach (var item in list) Console.WriteLine(item);
Output:
11 Standard(db=S,api=Standard) UI 1(db=1,api=STANDARD)
PS Of course, you can also write your parser:
static string[] MySplit(string dirty, char delimiter = ',', string ignoreInside = "()") { StringBuilder sb = new StringBuilder(); bool sectionStarted = false; List<string> result = new List<string>(); for (int i = 0; i < dirty.Length; i++) { if (!sectionStarted && dirty[i] == delimiter) { result.Add(sb.ToString()); sb.Clear(); continue; } if (ignoreInside.Contains(dirty[i])) sectionStarted = dirty[i] == ignoreInside[0]; sb.Append(dirty[i]); } return result.ToArray(); }
Usage: var result = MySplit(value);
The conclusion is the same as in the example above.
source share