views:

437

answers:

2

hi,

hopefully this should be an easy question. In java i know it's compareto, i think.

how do i compare 2 string variables to determine if they are the same?

ie:

if(string1 = string2  AND string3= string 4)  then

perform operation


else 

perform another operation


end if

Thanks

+2  A: 

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
Fredrik Mörk
+3  A: 
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)
  ' do something
else
  ' do something else
end if
Robert Harvey
good old string.equals Thank you!
this does not work! sorry!
I got it from here:http://msdn.microsoft.com/en-us/library/fbh501kz(VS.80).aspx
Robert Harvey