C# 中的字符串比较
我想比较两个字符串的任何匹配
,即;
我的两个字符串是
string1 = "hi i'm tibinmathew @ i'm fine";
string2 = "tibin";
我想比较上面的两个字符串。
如果找到任何匹配项,我必须执行一些语句。
我想在 c# 中执行此操作。我该怎么做?
I want to compare two strings for any match
ie;
my two string are
string1 = "hi i'm tibinmathew @ i'm fine";
string2 = "tibin";
I want to compare the above two string.
If there is any match found i have to execute some statements.
I want to do this in c#. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
比如下面这样的?
对于简单的子字符串,这是可行的。还有诸如
StartsWith
和EndsWith
之类的方法。对于更复杂的匹配,您可能需要正则表达式:
Such as the following?
For simple substrings this works. There are also methods like
StartsWith
andEndsWith
.For more elaborate matches you may need regular expressions:
看起来您只是想查看第一个字符串是否有与第二个字符串匹配的子字符串(无论在其中的任何位置)。你可以这样做:
It looks like you're just wanting to see if the first string has a substring that matches the second string, anywhere at all inside it. You can do this:
string1.Contains(string2)
最好地回答这个问题。string1.Contains(string2)
best answers this.如果您还想要匹配的位置,您可以执行正则表达式,或者
如果 string2 不在 string1 中则简单地返回 -1,忽略大小写。
If you want the position of the match as well, you could either do the regex, or simply
returns -1 if string2 is not in string1, ignoring casing.