2017-02-06 49 views
-2

我是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(); 
    } 
}  

回答

0

爲了使它正確,你應該發佈你的文本文件的內容。 反正你可以使用以下禁止code:A14並僅選擇code:A16code:A1

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

ty for your ur for its fine fine for me –

0
如果你想完全一樣的價值觀,並有行內沒有多餘的內容

你可以使用等於,

linee.Equals (「code:A1」)

相關問題