2012-08-03 77 views
0

我試圖使用正則表達式及其工作,但在這種情況下,只有當我用大寫字母輸入「NVIDIA」時。 我不介意以大寫字母顯示label4中的內容。 但是,如果變量toPring將具有「nvidia」或「NvIdiA」,它將自我搜索它不會顯示正則表達式知道如果它的大寫或不是它的特定文本。如何解析字符串並查找特定文本?

有沒有什麼辦法讓它知道以任何方式找到文本我在toPrint中鍵入它?

private void videoCardType() 
     { 
      string graphicsCard = string.Empty; 
      foreach (ManagementObject mo in searcher.Get()) 
      { 
       foreach (PropertyData property in mo.Properties) 
       { 
        if (property.Name == "Description") 
        { 
         graphicsCard = property.Value.ToString(); 
        } 
       } 
      } 

      string toPring = "NVIDIa"; 
      foreach (Match m in Regex.Matches(graphicsCard, toPring, RegexOptions.IgnoreCase)) 
      label4.Text = toPring; 
     } 

的問題是,如果我把toPrint「NVIDIA」,然後在label4文本將是「NVIDIA」小「一」,而在上述graphicsCard循環含有「NVIDIA GTX .... .. ..「

我想要的是,無論我在toPrint變量中鍵入,它都會在label4中顯示它,因爲它在graphicsCard變量中是原始的。

如果我在label4中鍵入toPring「NVIDIA」或「nvidia」或「NVidiA」,它將會像在顯卡中的「NVIDIA」顯卡一樣。

+1

因此,要解決這個問題,您需要首先意識到您的問題與字符串的[「case」](http://en.wikipedia.org/wiki/Letter_case)有關。然後意識到,當你檢查比賽時,你想*忽略*情況。然後,快速[Google搜索「C#regex ignore case」](http://www.google.com/search?q=c%23+regex+ignore+case)將直接引導您回答問題。 – 2012-08-03 01:29:24

回答

0

您還可以使用作爲模式:

string toPring = "(?i)NVIDIa"; 

玩得開心!

+0

解決了這個代碼:string graphicsCard =「... nvIDia ..... gtx ....」; string toPring =「NVIDIa」;如果(graphicsCard.ToUpper()。Contains(toPring.ToUpper())) int startingIndex = graphicsCard.ToUpper()。IndexOf(toPring.ToUpper());graphicsCard = graphicsCard.Substring(startingIndex,toPring.Length); } – user1544479 2012-08-03 06:03:00

相關問題