簡體   English   中英

搜索確切的字符串或跳過包含字符串的行

[英]Search for the exact string or skip the line that contains a string

我是 C# 新手,我遇到一個問題,當我搜索字符串"code:A1"它也會給出包含"code:A14" 我想搜索確切的單詞"code:A1"或只是從代碼中禁止"code:A14" 這是我所做的,但它不起作用:

try
{
    using (System.IO.StreamReader file = new System.IO.StreamReader(@"<path to file>"))
    {
        string motcletest = "code:A1";
        string motcle = "code:A16";
        string motcledm = "code:A14";
        string line;

        line = file.ReadLine();
        do
        {
            if (line.Contains(motcletest) || line.Contains(motcle))
            {
                SetupV02_textbox.Text = line;
            }
            if (line.Contains(motcledm))
            {
                continue;
            }
        }
        while ((line = file.ReadLine()) != null);

        //string retval = SetupV02_textbox.Text.Split("\n\r".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).First(p => p.Equals(motcle));
        string setup = SetupV02_textbox.Text;
        string testomada2 = setup.Split(new string[] { "code:" }, StringSplitOptions.None).Last();
        label2.Text = testomada2.ToString();
    }
}    

為了使其正確,您應該發布文本文件的內容。 無論如何,您可以使用以下code:A14禁用code:A14並僅選擇code:A16code:A1

if ((linee.Contains(motcletest) || linee.Contains(motcle)) && (!linee.Contains(motcledm)))
{
    SetupV02_textbox.Text = linee;
}

如果你想要完全相同的值並且行中沒有額外的內容,你可以使用 equals ,

linee.Equals("代碼:A1")

暫無
暫無

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

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