23

Hopefully this should be an easy question. In Java I think it's compareTo().

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

ie:

If (string1 = string2 And string3 = string4) Then
    'perform operation
Else
    'perform another operation
End If
Andreas
  • 5,393
  • 9
  • 44
  • 53

6 Answers6

25

I would suggest using the String.Compare method. Using that method you can also control whether 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
Andreas
  • 5,393
  • 9
  • 44
  • 53
Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
  • 11
    This answer is unnecessarily complicated. If case matters, simply do "str1 = str2". To ignore case, do "String.Equals(str1, str2, StringComparison.InvariantCultureIgnoreCase)". Or whichever StringComparison variant is appropriate to your task. No need to use `Compare`, which clutters code with `= 0`. And requires reader to think a bit more about the intent of the code. `Equals` makes the intent obvious. Only time I would use `Compare` is when the values other than `0` are needed (sorting). – ToolmakerSteve Apr 28 '14 at 00:47
  • 2
    It's interesting that `String.Compare(str1, str2)` casts to `False` when they're equal, and `True` when they're not equal. – Panzercrisis Mar 12 '15 at 16:14
  • 1
    @Panzercrisis you're telling me! I didn't read into this answer enough and did a ton of = 1 thinking = 1 casts to True...I'm gonna take ToolmakerSteve's advice on decluttering by using Equals. I don't see this as "interesting" like you said, I take it as extremely confusing and non intuitive if you've ever coded before, where 0 = false and 1 = true in almost all cases. – whyoz Jul 08 '16 at 19:03
  • 6
    There is nothing interesting or confusing about it. `String.Compare(str1, str2)` does not return a `Boolean`. `0` does not mean `False`, and `1` does not mean `True`. `String.Compare` returns an `Integer`: less than zero if `str1` comes before `str2` alphabetically, greater than zero if `str2` precedes `str1` alphabetically, and zero if the strings are the same. The returned value of `String.Compare` should never be cast to a `Boolean`. if you want a `Boolean`, use `String.Equals` which returns a `Boolean`. – Sam Hazleton Jan 17 '17 at 19:21
17

In vb.net you can actually compare strings with =. Even though String is a reference type, in vb.net = on String has been redefined to do a case-sensitive comparison of contents of the two strings.

You can test this with the following code. Note that I have taken one of the values from user input to ensure that the compiler cannot use the same reference for the two variables like the Java compiler would if variables were defined from the same string Literal. Run the program, type "This" and press <Enter>.

Sub Main()
    Dim a As String = New String("This")
    Dim b As String

    b = Console.ReadLine()

    If a = b Then
        Console.WriteLine("They are equal")
    Else
        Console.WriteLine("Not equal")
    End If
    Console.ReadLine()
End Sub
Tim
  • 1,755
  • 2
  • 22
  • 34
  • 1
    And even use `stringA <> stringB` to make sure they are different. Not sure if it's advisable but it seems to work fine. – MrCalvin Jun 27 '21 at 16:07
13
Dim MyString As String = "Hello World"
Dim YourString As String = "Hello World"
Console.WriteLine(String.Equals(MyString, YourString))

returns a bool True. This comparison is case-sensitive.

So in your example,

if String.Equals(string1, string2) and String.Equals(string3, string4) then
  ' do something
else
  ' do something else
end if
Peter B
  • 22,460
  • 5
  • 32
  • 69
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • I got it from here: http://msdn.microsoft.com/en-us/library/fbh501kz(VS.80).aspx – Robert Harvey May 23 '09 at 05:48
  • 4
    FYI, It works fine - I use it all the time. Though in vb.net and newer, one would typically use "AndAlso" rather than "And", to only do the second comparison if necessary ("short-circuit evaluation"). – ToolmakerSteve Apr 28 '14 at 00:34
  • What's the main advantage to using `String.Equals`, instead of `=`? I know you can only use the first one for a callback, but I wonder if there's more? – Panzercrisis Mar 12 '15 at 16:24
-1

I know this has been answered, but in VB.net above 2013 (the lowest I've personally used) you can just compare strings with an = operator. This is the easiest way.

So basically:

If string1 = string2 Then
    'do a thing
End If
-1

I think this String.Equals is what you need.

Dim aaa = "12/31"
            Dim a = String.Equals(aaa, "06/30")

a will return false.

Aman
  • 133
  • 1
  • 9
-2
If String.Compare(string1,string2,True) Then

    'perform operation

EndIf
kvorobiev
  • 5,012
  • 4
  • 29
  • 35
  • This doesn't seem to have nearly as much detail as the accepted answer from some years ago. Any particular reason to post it? – Nathan Tuggy Apr 11 '15 at 02:07