[英]Read just a part of a big string
I have a large string to read and it's always different, but one word is always the same. 我有一个大的字符串要读取,并且总是不同的,但是一个词总是相同的。 The word is MESSAGE
, so if my string reader comes across that word it has to write that whole string to disc. 这个单词是MESSAGE
,所以如果我的字符串阅读器遇到这个单词,则必须将整个字符串写到光盘上。 I did some code but it's not working, the if segment never triggers, what is wrong here? 我做了一些代码,但是它不起作用,如果if段永不触发,这是怎么回事?
string aLine;
StringReader strRead = new StringReader(str);
aLine = strRead.ReadLine();
if (aLine == "MESSAGE")
{
//Write the whole file on disc
}
You can use Contains, 您可以使用包含,
if(aLine.Contains("MESSAGE")
{
}
You can use String.Contains 您可以使用String.Contains
if (aLine.Contains("MESSAGE"))
You can also use String.IndexOf but as index is not relevant here so better to use Contains
. 您也可以使用String.IndexOf,但是由于索引与此处无关,因此最好使用Contains
。
if (aLine.IndexOf("MESSAGE") != -1)
If you need ignore case or Cultrue sensitivity then you IndexOf would provide you overloaded method for it, String.IndexOf(string value, StringComparison comparisonType) 如果您需要忽略大小写或Cultrue敏感性,则IndexOf将为其提供重载方法String.IndexOf(string value,StringComparison comparisonType)
if (aLine.IndexOf("MESSAGE", StringComparison.InvariantCultureIgnoreCase) != -1)
I think you might be looking for something like String.IndexOf . 我认为您可能正在寻找类似String.IndexOf的东西。 In that case you could use the following: 在这种情况下,您可以使用以下代码:
if (aLine.IndexOf("MESSAGE") > -1)
{
....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.