I have a string variable in C# aka product_name, I want to filter like this and have it return true or false:
product_name LIKE '%BONUS NAME%'
How can I do that please?
regards bk
PS: I have to do it on C# not SQL
I have a string variable in C# aka product_name, I want to filter like this and have it return true or false:
product_name LIKE '%BONUS NAME%'
How can I do that please?
regards bk
PS: I have to do it on C# not SQL
Simple Option: use String.Contains
http://msdn.microsoft.com/en-us/library/system.string.contains.aspx
Example from the MSDN page
// 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
*/
More robust: use regex library
http://msdn.microsoft.com/en-us/library/twcw2f1c.aspx
Regular expressions give you much more power, but you can mimic SQL's wildcards with regex operators '.' and '*'
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);
My preferred way of matching a part of a string is by using the IndexOf
method:
string input = "This is My FuLl string";
string lookFor = "full";
bool foundIt = input.IndexOf(lookFor, StringComparison.OrdinalIgnoreCase) >= 0;
Unlike Contains
this opens up for case insensitive matching.