I have a (ton of) string
(s) like this:
HKR,Drivers,SubClasses,,"wave,midi,mixer,aux"
I'm basically looking to split it into multiple strings
at the ,
character.
However, if the ,
character is inside "
or inside %
, it should be ignored.
In other words from the line above I'd expect the strings:
HKR
Drivers
SubClasses
"wave,midi,mixer,aux"
(with one empty string represented by the blank line above).
If the line was HKR,Drivers,SubClasses,,%wave,midi,mixer,aux%
then basically the same as above, only of course the last string to be returned should be %wave,midi,mixer,aux%
.
I have some working code, but it's incredibly slow to process all of the lines and I very much need to find a faster way to do this.
private static IEnumerable<string> GetValues(string line)
{
var insideQuotes = false;
var insidePercent = false;
var startValueIndex = 0;
for (var i = 0; i < line.Length; i++)
{
if (line[i] == '%' && !insideQuotes)
{
insidePercent = !insidePercent;
}
if (line[i] == '"')
{
insideQuotes = !insideQuotes;
}
if (line[i] != ',' || insideQuotes || insidePercent)
{
continue;
}
yield return line.Substring(startValueIndex, i - startValueIndex);
startValueIndex = i + 1;
}
}
Any help would be appreciated.