comparing strings in vb

前端 未结 6 812
南方客
南方客 2021-01-17 08:06

Hopefully this should be an easy question. In java i think it\'s compareTo().

How do I compare 2 string variables to determine if they are the same?

6条回答
  •  迷失自我
    2021-01-17 08:51

    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.

    Sample:

    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
    

    Edit: 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
    

提交回复
热议问题