10

Did C# provide any method to compare the string with a wildcard pattern like. Or I can say I want to find a "Like Operator" to do string comparison. Suppose I have a string .I also have a paragraph , I want to find the string on this parapgraph,But how.In SQL we can do it just using the LIKE operator.

Any Suggestion and reply is grateful.

shamim
  • 6,640
  • 20
  • 85
  • 151
  • 1
    possible duplicate of [System.StringComparer that supports wildcard (*) ](http://stackoverflow.com/questions/2433998/system-stringcomparer-that-supports-wildcard) – Gabe Aug 01 '10 at 05:02

3 Answers3

12

Wildcards are a complicated beast (a form of regular expressions), but it sounds like you want the Contains method. You can just do paragraph.Contains(sentence).

Gabe
  • 84,912
  • 12
  • 139
  • 238
6

String has a Contains method that should suffice, returns a boolean

"Big string that represents a paragraph".Contains("that");

Example from the Contains Method 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
*/

If you need more advanced matching then Regex might be the correct route, but from the example you stated I think contains will work fine.

Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
5

You can use Regex to define wildcards. These don't work exactly like the DOS ones, but are more powerful. See:

http://msdn.microsoft.com/en-us/library/ms228595(VS.80).aspx