0

我有一个大字符串要阅读,它总是不同的,但一个词总是相同的。这个词是MESSAGE,所以如果我的字符串阅读器遇到这个词,它必须将整个字符串写入磁盘。我做了一些代码,但它不起作用,if 段永远不会触发,这里有什么问题?

string aLine;
StringReader strRead = new StringReader(str);
aLine = strRead.ReadLine();

if (aLine == "MESSAGE")
{
    //Write the whole file on disc
}
4

4 回答 4

6

您可以使用包含,

if(aLine.Contains("MESSAGE")
{
}
于 2014-07-02T09:11:18.073 回答
2

您可以使用String.Contains

if (aLine.Contains("MESSAGE"))

您也可以使用String.IndexOf但因为 index 在这里不相关所以最好使用Contains.

if (aLine.IndexOf("MESSAGE") != -1)

如果您需要忽略大小写或文化敏感性,那么您 IndexOf 将为您提供重载方法String.IndexOf(string value, StringComparison comparisonType)

if (aLine.IndexOf("MESSAGE", StringComparison.InvariantCultureIgnoreCase) != -1)
于 2014-07-02T09:09:42.013 回答
1

您可以更改代码以使用Contains

            string aLine;
            StringReader strRead = new StringReader(str);
            aLine = strRead.ReadLine();

            if (aLine.Contains("MESSAGE"))
            {

              //Write the whole file on disc

            }
于 2014-07-02T09:10:36.867 回答
0

我想你可能正在寻找类似String.IndexOf的东西。在这种情况下,您可以使用以下内容:

if (aLine.IndexOf("MESSAGE") > -1)
{
    ....
}
于 2014-07-02T09:11:40.250 回答