locked
String.equals() and apostrophe RRS feed

  • Question

  • User1399716142 posted

    I just ran into what I consider a very strange issue in the execution of some code that I thought I had completely well-understood. Basically, I was using this kind of notation:

    If strOne.equals(strTwo) Then

    rather than

    If strOne = strTwo Then

    I came from a c and java background, and using the '=' operator for comparison has always made me cringe, so I've used the .equals() comparison function thinking that it was equivalent.

    But just now some of my code broke. It encountered a string containing "Eagle's Nest: Dawn Patrol", used the .equals() to compare it to "Eagle's Nest: Dawn Patrol" and declared them not equal. I changed the code to use '=' rather than '.equals()' and it declared them equal. I also did this:

    If strOne.Replace("'", "").Equals(strTwo.Replace("'", "")) Then

    and again, it declared them equal.

    This has me a little rattled because my code is filled with the use of .equals(), and I've used it assuming that it was no different than writing strOne = strTwo. I've scanned the interweb looking for information about this. I have not found any information about odd interactions between the apostrophe (or other) character(s) and .equals(). Microsoft's documentation does not hint at any subtle differences.

    Anybody out there who can tell me how .equals() differs from '=' will have my respect and gratitude. 

    Tom Purvis

    Wednesday, October 27, 2010 5:27 PM

Answers

  • User1006193418 posted

    Hi,

    Welcome to this forum.

    Here is the code for String.Equals() method:

    Public Function Equals(ByVal value As String) As Boolean
        If (Me Is Nothing) Then
            Throw New NullReferenceException
        End If
        If (value Is Nothing) Then
            Return False
        End If
        Return (Object.ReferenceEquals(Me, value) OrElse String.EqualsHelper(Me, value))
    End Function

    And the most important reference in this method is String.EqualsHelper which is as follows:

    Private Shared Function EqualsHelper(ByVal strA As String, ByVal strB As String) As Boolean
        Dim length As Integer = strA.Length
        If (length <> strB.Length) Then
            Return False
        End If
        Dim chRef As Char*
        Fixed chRef = AddressOf strA.m_firstChar
            Dim chRef2 As Char*
            Fixed chRef2 = AddressOf strB.m_firstChar
                Dim chPtr As Char* = chRef
                Dim chPtr2 As Char* = chRef2
                Do While (length >= 10)
                    If ((((DirectCast(chPtr, Integer*) <> DirectCast(chPtr2, Integer*)) OrElse (DirectCast((chPtr + 2), Integer*) <> DirectCast((chPtr2 + 2), Integer*))) OrElse ((DirectCast((chPtr + 4), Integer*) <> DirectCast((chPtr2 + 4), Integer*)) OrElse (DirectCast((chPtr + 6), Integer*) <> DirectCast((chPtr2 + 6), Integer*)))) OrElse (DirectCast((chPtr + 8), Integer*) <> DirectCast((chPtr2 + 8), Integer*))) Then
                        Exit Do
                    End If
                    chPtr = (chPtr + 10)
                    chPtr2 = (chPtr2 + 10)
                    length = (length - 10)
                Loop
                Do While (length > 0)
                    If (DirectCast(chPtr, Integer*) <> DirectCast(chPtr2, Integer*)) Then
                        Exit Do
                    End If
                    chPtr = (chPtr + 2)
                    chPtr2 = (chPtr2 + 2)
                    length = (length - 2)
                Loop
                Return (length <= 0)
            End Fixed
        End Fixed
    End Function

    However, after some tests, I didn't repro your issue. So for this odd problem, could you provided you code here for some further tests?

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 3, 2010 10:24 AM

All replies

  • User653228039 posted


    First, the assignment operator will always be true (unless you have a casting exception assigning two incompatible objects).  


    if(a = b)


    is not the same as the "is equal to" operator:


    if(a == b)


    That said, you have verified that your .equals works if only the apostrophe is removed?  You sure there aren't some spaces or other hard to see variables in there like \r\n ?



    Wednesday, October 27, 2010 6:51 PM
  • User1399716142 posted

    First, the assignment operator will always be true (unless you have a casting exception assigning two incompatible objects).  

    1. if(a = b)  
    if(a = b)

    is not the same as the "is equal to" operator:

    1. if(a == b)  
    if(a == b)

    That said, you have verified that your .equals works if only the apostrophe is removed?  You sure there aren't some spaces or other hard to see variables in there like \r\n ?

    Just to be clear, this is a question about Visual Basic, not c or C# or other c-like languages. Since it is posted to a form called Visual Basic .NET I assumed that was implied. 

    What you say about the assignment operator (=) vs the comparison (==) [you say "equal to" operator] is NOT true in Visual Basic. When the assignment operator (=) comes between an If and a Then in VB code it is a comparison operator. I just don't like to write it that way, since using the assignment operator in an if in java or c creates a bug--bad practice.

    The text was exactly the same. The code was comparing data in a file against data in a database. The string that came from the db had been written to it from this program reading the same file. When I simply changed the statement from strOne.equals(strTwo) to strOne = strTwo and ran the program against the same data, it did see them as equal. And when I compared the two strings with the apostrophe removed, using the exact same .Replace("'", "") against both string objects, .equals also found them to be a match.

    Wednesday, October 27, 2010 7:08 PM
  • User1399716142 posted

    I made a mistake on this. A problem came up in some quite complicated code, I think I got confused about what was happening. Thanks to anyone who looked at the issue.

    Tom Purvis

    Thursday, October 28, 2010 2:13 PM
  • User1006193418 posted

    Hi,

    Welcome to this forum.

    Here is the code for String.Equals() method:

    Public Function Equals(ByVal value As String) As Boolean
        If (Me Is Nothing) Then
            Throw New NullReferenceException
        End If
        If (value Is Nothing) Then
            Return False
        End If
        Return (Object.ReferenceEquals(Me, value) OrElse String.EqualsHelper(Me, value))
    End Function

    And the most important reference in this method is String.EqualsHelper which is as follows:

    Private Shared Function EqualsHelper(ByVal strA As String, ByVal strB As String) As Boolean
        Dim length As Integer = strA.Length
        If (length <> strB.Length) Then
            Return False
        End If
        Dim chRef As Char*
        Fixed chRef = AddressOf strA.m_firstChar
            Dim chRef2 As Char*
            Fixed chRef2 = AddressOf strB.m_firstChar
                Dim chPtr As Char* = chRef
                Dim chPtr2 As Char* = chRef2
                Do While (length >= 10)
                    If ((((DirectCast(chPtr, Integer*) <> DirectCast(chPtr2, Integer*)) OrElse (DirectCast((chPtr + 2), Integer*) <> DirectCast((chPtr2 + 2), Integer*))) OrElse ((DirectCast((chPtr + 4), Integer*) <> DirectCast((chPtr2 + 4), Integer*)) OrElse (DirectCast((chPtr + 6), Integer*) <> DirectCast((chPtr2 + 6), Integer*)))) OrElse (DirectCast((chPtr + 8), Integer*) <> DirectCast((chPtr2 + 8), Integer*))) Then
                        Exit Do
                    End If
                    chPtr = (chPtr + 10)
                    chPtr2 = (chPtr2 + 10)
                    length = (length - 10)
                Loop
                Do While (length > 0)
                    If (DirectCast(chPtr, Integer*) <> DirectCast(chPtr2, Integer*)) Then
                        Exit Do
                    End If
                    chPtr = (chPtr + 2)
                    chPtr2 = (chPtr2 + 2)
                    length = (length - 2)
                Loop
                Return (length <= 0)
            End Fixed
        End Fixed
    End Function

    However, after some tests, I didn't repro your issue. So for this odd problem, could you provided you code here for some further tests?

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 3, 2010 10:24 AM