Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Identical strings aren't String.Equals() - ?!

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
571
US
Colleagues,
I'm totally confused!
Please look at these strings and tell me if they aren't identical ("Locals" window screenshot taken during debug execution in IDE):

20200429_EqualStringsAreNotEqual_ddqxsb.jpg


They look 157.75% identical to yours truly!
However, the VB and .NET seem do not think so:

20200429_EqualStringsAreNotEqual_DebugRun_irftu7.jpg


So, as you can see, the strings are totally identical in the Locals window, and therefore the command pointer should've jumped from line #175 over to the line #177... but it dove into line 176 instead, and I dunno why.

Here's the code (sorry, had to obfuscate the real files' names):

Code:
Dim lcTestStr(38) As Char, lsTestStr As String = ""
Dim lsXMLDeclaration As String = "<?xml version=" & Chr(34) & "1.0" & Chr(34) & " encoding=" & Chr(34) & "UTF-8" & Chr(34) & "?>"

If File.Exists(pcDataDir & "Start.xml") Then

Dim loStreamReader As StreamReader = New StreamReader(pcDataDir & "Start_.xml")

   loStreamReader.Read(lcTestStr, 0, 38)
   lsTestStr = String.Join("", lcTestStr)

   If Not String.Equals(lsTestStr, lsXMLDeclaration) Then
      txtSrcXMLs.Text = ""
   End If

What am I doing wrong?

Regards,

Ilya
 
I think it is used like this:

Code:
Dim firstString As String = "test"
Dim secondString As String = "test"
If (firstString.Equals(secondString)) Then
   ' code
End If


Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
To MarkSweetland:
I tried it both ways, colleague, result was the same.
Besides, the MS documentation lists both syntax cases as legit.

Meanwhile, I found another String’s method that seems working (I think):

Code:
If Not (String.Compare(lsXMLDeclaration, lsTestStr) = 0) Then ' lsTestStr.Equals(lsXMLDeclaration) Then ' String.Equals(lsTestStr, lsXMLDeclaration) Then
   txtSrcXMLs.Text = ""
End If

"Whatd'ya think of that?" [wink] (Ian Gillan, "Anyone's Daughter", "Fireball", 1971)

Regards,

Ilya
 
And/or the embedded double quotes within the two string (per your example with Chr(34)). Try Chr(39) single quote.



Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
>I found another String’s method that seems working (I think)

Here's Microsoft's official stance on that:

Microsoft dotnet documentation said:
The String.Compare method is primarily intended for use when ordering or sorting strings. You should not use the String.Compare method to test for equality
 
To strongm: it seems that, despite this MS's recommendation, this String.Compare() method works properly and correctly, whereas String.Equals() does not.
So, what this poor old me is supposed to do?

Regards,

Ilya
 
Your original String.Equals is correctly showing that the strings do not match (as I say, check the length of both strings). String.compare suggesting they match is wrong ...
 


I think it may be your character array. Your declaration for the character array lcTestStr is actually 39 characters long (zero based) and contains a null character as the final character in the array after the StreamReader.Read() since you're only reading 38 characters. When you convert the character array to a string it also includes that null character, and does not match the other string.

Change
Dim lcTestStr(38) As Char
to
Dim lcTestStr(37) As Char

or you can replace the null characters using
lsTestStr = New String(lcTestStr).Replace(Convert.ToChar(0), "")


Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
You've spoiled it now, Mark ;-) Was hoping for Ilya to be able to work his way to that once he confirmed that the strings were different lengths (teaching a man to fish, and all that)
 

Oh well, sorry about that. I always seem to get deep into troubleshooting and get that "Ah Hah!" moment and get carried away.


Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Colleagues,
I thought I have it posted, but apparently it fell thru the cracks...
Yes, as my own investigation showed, the length was not equal: 38 in the built (for checking) string, 39 in the read string.
The code

Code:
loStreamReader.Read(lcTestStr, 0, 38)
lsTestStr = Strings.Left(String.Join("", lcTestStr), 38)

did the trick.
So, you are saying that VB is 0-based?
I vaguely recall that there was a global setting (or property?) in pre-.NET VB that allowed to switch to 1-based enumeration... Is it still there? (Just curious.)

Regards,

Ilya
 
In classic VB you had Option Base where you could change the base number of arrays. That capabliluity was removed in VB.NET.

Note, however, that Option Base 0 was the default in classic VB

>lsTestStr = Strings.Left(String.Join("", lcTestStr), 38)
Sure, but

Dim lcTestStr(37) As Char

is probably better
 
Option Base 0 was the default in classic VB" - yes, I recall that.
Thank you all, colleagues!
The issue's been resolved, case is closed.

Regards,

Ilya
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top