Odd string compare results

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

If I test "A+" > "A-" then I get true

If I test "A+A" > "A-A" then I get false

What the hell is going on?! I can only imagine it's down to culture... but
why? my thread culture setting is "en-GB"

What rules are going on here? Does this mean the only way I can ever test
strings is to use String.CompareOrdinal ??????

HELP!
 
Gwyn said:
If I test "A+" > "A-" then I get true

If I test "A+A" > "A-A" then I get false

What the hell is going on?! I can only imagine it's down to culture... but
why? my thread culture setting is "en-GB"

What rules are going on here? Does this mean the only way I can ever test
strings is to use String.CompareOrdinal ??????

I suspect it's viewing '-' as a character which can join two words
together, but '+' as just whitespace, so

foo+bar > foo-bar > foobar

Just a guess though - I don't have my laptop at the moment and can't
check.

As for how you should test strings - it entirely depends on the
context. If you want to compare them in a culture-sensitive way, then
expect results like the above. If you want to sort them in a
culture-insensitive way, use String.CompareOrdinal.

See
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/StringsinNET20.asp
for more on this.

Jon
 
Gwyn said:
If I test "A+" > "A-" then I get true

If I test "A+A" > "A-A" then I get false

What the hell is going on?! I can only imagine it's down to culture... but
why? my thread culture setting is "en-GB"

What rules are going on here? Does this mean the only way I can ever test
strings is to use String.CompareOrdinal ??????

HELP!

I you want to be explicit, remember a string is an array of characters,
so you can always do individual tests on each element.

In fact, why not use that method in your own class and implement
IComparable?
 
Hi,

in your form/module/class set your option Compare to binary

Option Explicit On
Option Strict On
Option Compare Binary

and that should do it (even though the default is supposed to be
Binary - but it can be changed in tools/options/projects/VBDefaults)


Barry

If I test "A+" > "A-" then I get true

If I test "A+A" > "A-A" then I get false

What the hell is going on?! I can only imagine it's down to culture... but
why? my thread culture setting is "en-GB"

What rules are going on here? Does this mean the only way I can ever test
strings is to use String.CompareOrdinal ??????

HELP!

bceggersATcomcastDOTnet
 
Barry said:
in your form/module/class set your option Compare to binary

Option Explicit On
Option Strict On
Option Compare Binary

and that should do it (even though the default is supposed to be
Binary - but it can be changed in tools/options/projects/VBDefaults)

That doesn't affect the results of String.Compare, only the VB string
comparison functions. Here's proof:

Option Explicit On
Option Strict On
Option Compare Binary

Imports System

Class Test

Shared Sub Main()
Console.WriteLine ("A+".CompareTo("A-"))
Console.WriteLine ("A+A".CompareTo("A-A"))
End Sub

End Class

That prints out 1 then -1.
 
My Bad,
I assumed the OP was using the VB operators...
If I test "A+" > "A-" then I get true

If I test "A+A" > "A-A" then I get false

Apologies if I assumed wrong.


That doesn't affect the results of String.Compare, only the VB string
comparison functions. Here's proof:

Option Explicit On
Option Strict On
Option Compare Binary

Imports System

Class Test

Shared Sub Main()
Console.WriteLine ("A+".CompareTo("A-"))
Console.WriteLine ("A+A".CompareTo("A-A"))
End Sub

End Class

That prints out 1 then -1.

bceggersATcomcastDOTnet
 
Barry said:
My Bad,
I assumed the OP was using the VB operators...

Apologies if I assumed wrong.

No, I suspect you're right - apologies for *my* assumption...
 
Back
Top