如何使用SQL中的LIKE逻辑使用c#过滤字符串?

Bil*_*lıç 2 c# string filter

我在C#aka product_name中有一个字符串变量,我想像这样过滤并让它返回true或false:

product_name LIKE '%BONUS NAME%' 
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

PS:我必须在C#而不是SQL上做

vfi*_*lby 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
*/
Run Code Online (Sandbox Code Playgroud)

更强大:使用正则表达式库

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);
Run Code Online (Sandbox Code Playgroud)