我有以下用于 csv 解析器的代码
string input = wholeFile;
IList<string> wholeFileArray = new List<string>();
int start = 0;
bool inQuotes = false;
for (int current = 0; current < input.Length; current++)
{
// test each character before and after to determine if it is a valid quote, or a quote within a quote.
int test_backward = (current == 0 ? 1 : current) - 1;
int test_forward = (current == input.Length - 1 ? input.Length - 2 : current) + 1;
bool valid_quote = input[test_backward] == ',' || input[test_forward] == ',' || input[test_forward] == '\r';
if (input[current] == '\"') // toggle state
{
inQuotes = !inQuotes;
}
bool atLastChar = (current == input.Length - 1);
if (atLastChar)
{
wholeFileArray.Add(input.Substring(start));
}
else if (input[current] == ',' && !inQuotes)
{
wholeFileArray.Add(input.Substring(start, current - start));
start = current + 1;
}
}
,
如果它,
不在这样的双引号字符串中,它需要一个字符串并将其拆分"something,foobar"
。
我的问题是"
我的字符串中的流氓正在搞乱我的整个过程。
例如:"bla bla","bla bla2",3,4,"5","bla"bla","End"
结果
- “布拉布拉”
- “布拉布拉2”
- 3
- 4
- “5”
- "bla"bla","结束"
如何更改我的代码以允许流氓"
一个“有效”的右引号总是后跟一个逗号 (,) 或一个控制换行符
添加 了这似乎可以解决它
// test each character before and after to determine if it is a valid quote, or a quote within a quote.
int test_backward = (current == 0 ? 1 : current) - 1;
int test_forward = (current == input.Length - 1 ? input.Length - 2 : current) + 1;
bool valid_quote = input[test_backward] == ',' || input[test_forward] == ',' || input[test_forward] == '\r';