c#中的字符串比较

tib*_*hew 2 c# string compare

我想比较任何匹配的两个字符串

即;

我的两个字符串是

string1 = "hi i'm tibinmathew @ i'm fine";

string2 = "tibin";
Run Code Online (Sandbox Code Playgroud)

我想比较上面的两个字符串.

如果发现任何匹配,我必须执行一些陈述.

我想在c#中这样做.我怎样才能做到这一点?

Joe*_*oey 11

如下?

string1 = "hi i'm tibinmathew @ i'm fine";
string2 = "tibin";

if (string1.Contains(string2))
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

对于简单的子串,这是有效的.还有像StartsWith和的方法EndsWith.

对于更精细的匹配,您可能需要正则表达式:

Regex re = new Regex(@"hi.*I'm fine", RegexOptions.IgnoreCase);

if (re.Match(string1))
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)


wom*_*omp 6

看起来你只是想看看第一个字符串是否有一个匹配第二个字符串的子字符串,在它里面的任何地方.你可以这样做:

if (string1.Contains(string2))
{
   // Do stuff
}
Run Code Online (Sandbox Code Playgroud)


Suk*_*asa 6

if (string1.Contains(string2)) {
    //Your code here
}
Run Code Online (Sandbox Code Playgroud)