2010-04-06 43 views

回答

4
if(product_name.Contains("BONUS NAME")) 
    // do stuff.. 
14

簡單的選擇:使用String.Contains

http://msdn.microsoft.com/en-us/library/system.string.contains.aspx

從MSDN頁面

// This example demonstrates the String.Contains() method 
using System; 

class Sample 
{ 
    public static void Main() 
    { 
    string s1 = "The quick brown fox jumps over the lazy dog"; 
    string s2 = "fox"; 
    bool b; 
    b = s1.Contains(s2); 
    Console.WriteLine("Is the string, s2, in the string, s1?: {0}", b); 
    } 
} 
/* 
This example produces the following results: 

Is the string, s2, in the string, s1?: True 
*/ 

例更強大:使用正則表達式庫

http://msdn.microsoft.com/en-us/library/twcw2f1c.aspx

正則表達式給你更多的權力,但你可以模仿SQL的通配符正則表達式運算符「」和「*」

string text = "The quick brown fox jumps over the lazy dog"; 
string pat = @"fox"; 

// Instantiate the regular expression object. 
Regex r = new Regex(pat, RegexOptions.IgnoreCase); 

// Match the regular expression pattern against a text string. 
Match m = r.Match(text); 
2

我的優選匹配的字符串的一部分的方法是通過使用該方法IndexOf

string input = "This is My FuLl string"; 
string lookFor = "full"; 
bool foundIt = input.IndexOf(lookFor, StringComparison.OrdinalIgnoreCase) >= 0; 

不同於Contains這開闢了不區分大小寫匹配。

相關問題