使用string.text.contains时如何忽略大小写? [英] How to ignorecase when using string.text.contains?

查看:54
本文介绍了使用string.text.contains时如何忽略大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何在使用.text.contains忽略大小写的同时检查字符串是否包含另一个字符串.

I am trying to figure out how to check if a string contains another while ignoring case using .text.contains.

现在的状态,如果我这样做:

As it stands right now If I do this:

 Dim myhousestring As String = "My house is cold"
    If txt.Text.Contains(myhousestring) Then
    Messagebox.Show("Found it")
    End If

仅在完全相同的情况下才返回匹配项.因此,如果用户键入我的房子很冷",那将不是一个匹配项.

It will only return a match if it is the exact same case. So if the user typed "my house is cold", it would not be a match.

我该怎么做?如果不可能的话,我可能只用regex代替ignorecase.任何帮助将不胜感激.

How can I do this? If it is not possible I could probably just use regex instead with ignorecase. Any help would be appreciated.

推荐答案

根据 Microsoft 您可以使用 IndexOf 而不是 Contains 在字符串中进行不区分大小写的搜索.因此,当 IndexOf 方法的结果返回的值大于 -1 时,这意味着第二个字符串是第一个字符串的子字符串.

According to Microsoft you can do case-insensitive searches in strings with IndexOf instead of Contains. So when the result of the IndexOf method returns a value greater than -1, it means the second string is a substring of the first one.

Dim myhousestring As String = "My house is cold"
If txt.Text.IndexOf(myhousestring, 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then
    Messagebox.Show("Found it")
End If

您还可以使用 StringComparison 的其他不区分大小写的变体.

You can also use other case-insensitive variants of StringComparison.

这篇关于使用string.text.contains时如何忽略大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆