.net 使用 string.text.contains 时如何忽略大小写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14064189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to ignorecase when using string.text.contains?
提问by user1632018
I am trying to figure out how to check if a string contains another while ignoring case using .text.contains.
我试图弄清楚如何使用 .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.
如果情况完全相同,它只会返回匹配项。因此,如果用户输入“我的房子很冷”,则不会匹配。
How can I do this? If it is not possible I could probably just use regex instead with ignorecase. Any help would be appreciated.
我怎样才能做到这一点?如果不可能,我可能只使用正则表达式而不是忽略大小写。任何帮助,将不胜感激。
回答by Marcel Gosselin
According to Microsoftyou can do case-insensitive searches in strings with IndexOfinstead of Contains. So when the result of the IndexOfmethod returns a value greater than -1, it means the second string is a substring of the first one.
根据微软的说法,你可以在字符串中进行不区分大小写的搜索,IndexOf而不是Contains. 因此,当该IndexOf方法的结果返回大于 的值时-1,表示第二个字符串是第一个字符串的子字符串。
Dim myhousestring As String = "My house is cold"
If txt.Text.IndexOf(myhousestring, 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then
Messagebox.Show("Found it")
End If
You can also use other case-insensitive variants of StringComparison.
您还可以使用StringComparison 的其他不区分大小写的变体。
回答by Snakes and Coffee
I'm not a vb.net programmer, but according to Microsoft, you can get the lowercase/uppercase value of the text using the string methods ToUpper()or ToLower(). You can then compare that with "my house is cold"or "MY HOUSE IS COLD".
我不是 vb.net 程序员,但根据Microsoft 的说法,您可以使用字符串方法ToUpper()或ToLower(). 然后,您可以将其与"my house is cold"或进行比较"MY HOUSE IS COLD"。
Dim myhousestring As String = "MY HOUSE IS COLD"
If txt.Text.ToUpper.Contains(myhousestring) Then
Messagebox.Show("Found it")
End If
回答by Adam Pine
Personally I just used:
我个人刚刚使用:
item.Text.ToLower().Contains("my house is cold")
you could just as well use ToUpper as well.
您也可以使用 ToUpper。
Caveat: If you are comparing Turkish, or other languages, the ToLower() and ToUpper() also take an option parameter, for "CultureInfo" allowing you to ensure that different languages are handled correctly. You can use an above solution, or you can follow the steps from Microsoft's ToLower Documentation, to add in CultureInfo, to get ToLower context on which language you are about to try to manipulate.
警告:如果您正在比较土耳其语或其他语言,ToLower() 和 ToUpper() 还带有一个选项参数,用于“CultureInfo”,允许您确保正确处理不同的语言。您可以使用上述解决方案,也可以按照 Microsoft 的 ToLower 文档中的步骤添加 CultureInfo,以获取有关您将要尝试操作的语言的 ToLower 上下文。
ToLower() with CultureInfo documentation
回答by xcrookedxedge
I solved this problem with .toUpper
我用 .toUpper 解决了这个问题
For example:
例如:
Dim UGroup as String = dr.Item(2).ToString().ToUpper
Dim s as String = ds.Item(1).ToString.ToUpper
If s.Contains(UGroup) then MsgBox("Well done!")
Else
End Sub
Same procedure with .toLower
与 .toLower 相同的程序
回答by Sandy
use the InStr example. "contains" fails if ether compare is nothing.
使用 InStr 示例。如果以太比较什么都没有,则“包含”失败。
'if we found something...
'如果我们发现了什么......
If InStr(1, value, search, vbTextCompare) > 0 Then
Beep
End If
'
回答by beppe9000
What about this?
那这个呢?
<Runtime.CompilerServices.Extension>
Function InStr(s As String, find As String) As Boolean
Return s.ToLower.Contains(find.ToLower)
End Function
回答by Elias
this is how I solved my problem of making String.Contains become case insensitive.
这就是我如何解决使 String.Contains 变得不区分大小写的问题的方法。
Dim s as string = "My HoUsE iS cOlD".ToUpper
If s.Contains("MY HOUSE IS COLD") Then Exit Sub
For my particular issue, the string that I was checking was housed within a TextBox.
对于我的特定问题,我正在检查的字符串位于 TextBox 中。
I hope this helps.
我希望这有帮助。
回答by Muteb A.
Or you can use RegularExpressions like this.
或者你可以像这样使用正则表达式。
First, import the RegularExpressions:
首先,导入正则表达式:
Imports System.Text.RegularExpressions
then try this code:
然后试试这个代码:
Dim match As Match = Regex.Match(Textbox1.text,"My house is cold",RegexOptions.IgnoreCase)
If match.Success Then
Msgbox(match.Value)
End If

