我很可能遇到了一个逻辑问题。我正在使用C#进行编码,但欢迎提供一般的伪代码解决方案。
我有这些文本文件,例如包含以下文本:
blah "hello john"
blah 'the code is "flower" '
blah "good night"
我想循环遍历双引号并对其进行某些操作,但我希望忽略单引号中包含的双引号。我通过以下方式(使用包含文本文件内容的
string data
)获取开头和结尾双引号的位置:
C#
// Start searching from beginning
int quotestart = 0, quoteend = 0;
while (data.IndexOf('"', quotestart) != -1)
{
// Get opening double quote
quotestart = data.IndexOf('"', quotestart);
// Get ending double quote
quoteend = data.IndexOf('"', quotestart + 1);
string sub = data.Substring(quotestart + 1, quoteend - quotestart - 1);
Console.WriteLine(sub);
// Set the start position for the next round
quotestart = quoteend + 1;
}
使用我的代码,输出将是:
hello john
flower
good night
因为“flower”在单引号内,所以我希望我的输出结果是:
hello john
good night
编辑
我目前正在研究一种方法,即首先使用例如“ A”填充单引号之间的所有数据。这样,当我遍历双引号时,任何在单引号之间的数据都将被忽略。不确定这是否是正确的方法。
1 " 2 ' 3 " 4 " 5 ' 6 " 7
这个怎么处理呢?你想忽略掉引号外面的 1,然后是要输出2 ' 3
再忽略掉 4,输出5 ' 6
,还是认为单引号里面的都要被忽略,输出2 6
? - Eric Lippert