/ Published in: C#
it ignores commas in a csv that are in string quotes
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
public static string[] SanitizeSplit(string s, string seperator) { bool mustSplit = true; string currentWord = string.Empty; for (int i = 0; i < s.Length; i++) { string l = s.Substring(i, 1); if ((l == "'" || l == "\"") && mustSplit) { mustSplit = false; continue; } if ((l == "'" || l == "\"") && !mustSplit) { mustSplit = true; continue; } if (l != seperator || (l == seperator && !mustSplit)) currentWord += l; else { result.Add(currentWord.Trim()); currentWord = string.Empty; } } result.Add(currentWord); return result.ToArray(); }