开发者

How to do string comparison with wildcard pattern in C#

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.


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).


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜