Last active
August 19, 2017 07:25
-
-
Save NNNIC/05271fc686a3d1186b9adfe3336e7416 to your computer and use it in GitHub Desktop.
Split string by comma considering if commas exist between pair of parentheses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static List<string> Split(string val) | |
{ | |
if (string.IsNullOrEmpty(val)) return null; | |
var list = new List<string>(); | |
char[] pair_start = new char[] { '(','[','{' }; | |
char[] pair_end = new char[] { ')',']','}' }; | |
var stack = new List<char>(); | |
Func<char> expect_endchar = ()=> stack.Count>0 ? stack[0] : (char)0; | |
var word = string.Empty; | |
for(var i = 0; i<val.Length; i++) | |
{ | |
var c = val[i]; | |
if (expect_endchar()==0 && (c==',')) | |
{ | |
list.Add(word); | |
word = string.Empty; | |
continue; | |
} | |
var pair_start_index = Array.FindIndex(pair_start,v=>v==c); | |
if (pair_start_index>=0) | |
{ | |
var endchar = pair_end[pair_start_index]; | |
stack.Insert(0,endchar); | |
} | |
else if (c==expect_endchar()) | |
{ | |
stack.RemoveAt(0); | |
} | |
word += c; | |
} | |
if (!string.IsNullOrEmpty(word)) list.Add(word); | |
return list; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment