Comparing strings using equal to operator is an overhead especially when you try to change case and compare, instead string.compare could be used.
Here I used ConvertToString function mentioned earlier in the blog, so it could handle null values also.
Function returns true, strings are same, else different
Public Shared Function StringCompareNoCase(ByVal object1 As Object, _
ByVal object2 As Object) As Boolean
Dim string1 As String = String.Empty
Dim string2 As String = String.Empty
Try
string1 = ConvertToString(object1)
string2 = ConvertToString(object2)
If String.Compare(string1, string2, True) = 0 Then
Return True
Else
Return False
End If
Catch
Return False
End Try
End Function
Public Shared Function StringCompareNoCase(ByVal string1 As String, _
ByVal string2 As String) As Boolean
Try
If String.Compare(string1, string2, True) = 0 Then
Return True
Else
Return False
End If
Catch
Return False
End Try
End Function
Public Shared Function StringCompare(ByVal object1 As Object, _
ByVal object2 As Object) As Boolean
Dim string1 As String = String.Empty
Dim string2 As String = String.Empty
Try
string1 = ConvertToString(object1)
string2 = ConvertToString(object2)
If String.Compare(string1, string2, False) = 0 Then
Return True
Else
Return False
End If
Catch
Return False
End Try
End Function
Public Shared Function StringCompare(ByVal string1 As String, _
ByVal string2 As String) As Boolean
Try
If String.Compare(string1, string2, False) = 0 Then
Return True
Else
Return False
End If
Catch
Return False
End Try
End Function
No comments:
Post a Comment