簡體   English   中英

僅讀取大字符串的一部分

[英]Read just a part of a big string

我有一個大的字符串要讀取,並且總是不同的,但是一個詞總是相同的。 這個單詞是MESSAGE ,所以如果我的字符串閱讀器遇到這個單詞,則必須將整個字符串寫到光盤上。 我做了一些代碼,但是它不起作用,如果if段永不觸發,這是怎么回事?

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

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

您可以使用包含,

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

您可以使用String.Contains

if (aLine.Contains("MESSAGE"))

您也可以使用String.IndexOf,但是由於索引與此處無關,因此最好使用Contains

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

如果您需要忽略大小寫或Cultrue敏感性,則IndexOf將為其提供重載方法String.IndexOf(string value,StringComparison comparisonType)

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

您可以更改代碼以使用Contains

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

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

              //Write the whole file on disc

            }

我認為您可能正在尋找類似String.IndexOf的東西。 在這種情況下,您可以使用以下代碼:

if (aLine.IndexOf("MESSAGE") > -1)
{
    ....
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM