Below two which is the best practise to compare strings...
Code:If "A".ToLower = "a" Then
'....
End If
If String.Compare("A", "A", True) = 0 Then
'...
End If
Printable View
Below two which is the best practise to compare strings...
Code:If "A".ToLower = "a" Then
'....
End If
If String.Compare("A", "A", True) = 0 Then
'...
End If
The first example you provided isn't a true comparison as for example you are changing what the user entered into a lowercase; no longer representing what was entered. A true comparison would be does "A" = "A". So based on that the second example would have to win as it is a comparison IMHO.
I always use String.Equals as it returns a boolean and you do not have to compare to 0. Additionalty you can define how to compare the values.
Code:If String.Equals("A", "a", StringComparison.OrdinalIgnoreCase) Then
...
End If