×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
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):



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



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 --> VB.NET

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

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

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

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

(OP)
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 --> VB.NET

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

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

You might want to check the length of both strings.

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

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

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

Nothing to do with the embedded quotes

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

>I found another String’s method that seems working (I think)

Here's Microsoft's official stance on that:

Quote (Microsoft dotnet documentation)


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

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

(OP)
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

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

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 ...

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



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

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

You've spoiled it now, Mark winky smile 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)

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


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

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

(OP)
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 --> .NET

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

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

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

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

(OP)
"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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close