2013-05-28 12 views
0

我的代碼有一個小問題,我正在閱讀文本文件並顯示來自用戶輸入的匹配項。唯一的問題是它是區分大小寫的,例如,如果Steve中的S是小寫,那麼它將不會顯示匹配,因爲它在文本File中是大寫。這是我正在使用的代碼。如何從文本文件中讀取並顯示沒有匹配大小寫的匹配項?

string name; 

lstResult.Items.Clear(); 

using (StreamReader sr = File.OpenText("../Name_Check.txt")) 
{     
    while ((name = sr.ReadLine()) != null) 
    {      
     if (txtInput.Text == name) 
     {      
      lstResult.Items.Add(name); 

回答

2

可以使用String.Equals(string, string, StringComparison)代替==

對於你的榜樣,這應該工作:

if (string.Equals(txtInput.Text, name, StringComparison.CurrentCultureIgnoreCase)) 

(而不是if (txtInput.Text == name)

這假設您想要使用的當前線程的當前文化設置。

或者您可以使用Daniel White演示的類似string.Equals()。

+0

感謝您的快速響應,完美的作品:) – UberG00n

4

試試這個

txtInput.Text.Equals(name, StringComparision.OrdinalIgnoreCase) 

您可能需要更改取決於你的文化的最後一個選項。

0

這應該工作(沒有測試!)

string name; 
lstResult.Items.Clear(); 
using (StreamReader sr = File.OpenText("../Name_Check.txt")) 
{ 
    while ((name = sr.ReadLine()) != null) 
    { 
     if (txtInput.Text.Equals(name, StringComparison.InvariantCultureIgnoreCase)) 
     { 
      lstResult.Items.Add(name); 
相關問題