且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 VB.NET 中比较字符串

更新时间:2023-02-20 11:57:14

我建议使用 String.Compare 方法.使用该方法,您还可以控制是否让它执行区分大小写的比较.

I would suggest using the String.Compare method. Using that method you can also control whether to to have it perform case-sensitive comparisons or not.

示例:

Dim str1 As String = "String one"
Dim str2 As String = str1
Dim str3 As String = "String three"
Dim str4 As String = str3

If String.Compare(str1, str2) = 0 And String.Compare(str3, str4) = 0 Then
    MessageBox.Show("str1 = str2 And str3 = str4")
Else
    MessageBox.Show("Else")
End If

如果要执行不区分大小写的搜索,可以使用 StringComparison 参数:

if you want to perform a case-insensitive search you can use the StringComparison parameter:

If String.Compare(str1, str2, StringComparison.InvariantCultureIgnoreCase) = 0 And String.Compare(str3, str4, StringComparison.InvariantCultureIgnoreCase) = 0 Then