473,320 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Odd string compare results

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!
Jan 23 '06 #1
6 1947
Gwyn wrote:
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/de...ngsinNET20.asp
for more on this.

Jon

Jan 23 '06 #2
Gwyn wrote:
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?

Jan 23 '06 #3
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

On Mon, 23 Jan 2006 06:43:03 -0800, "Gwyn"
<Gw**@discussions.microsoft.com> wrote:
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
Jan 26 '06 #4
Barry <bc******@castcom.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 26 '06 #5
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.
On Thu, 26 Jan 2006 07:30:17 -0000, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
Barry <bc******@castcom.com> wrote:
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.


bceggersATcomcastDOTnet
Jan 27 '06 #6
Barry <bc******@castcom.com> wrote:
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.


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 27 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Kamilche | last post by:
I want to convert a dict into string form, then back again. After discovering that eval is insecure, I wrote some code to roll a Python object, dict, tuple, or list into a string. I've posted it...
6
by: Robin Siebler | last post by:
I have two directory trees that I want to compare and I'm trying to figure out what the best way of doing this would be. I am using walk to get a list of all of the files in each directory. I...
0
by: g | last post by:
Hi all. Maybe this question has been asked many times before, but I was not able to find any pointer. I apologize in advance, for it refers to a particular standard library implementation (GNU...
3
by: tvn007 | last post by:
Please help, I am not sure why this code not even compile due to line 39 and 44. Thanks in advance for any help 1 #include <cctype> 2 #include <iostream> 3 #include <string> 4 #include...
19
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I...
10
by: lchian | last post by:
Hi, For two stl strings s1 and s2, I got different results from strcmp(s1.c_str(), s2.c_str()) and s1.compare(s2) can someone explain what these functions do? It seems that strcmp gives...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
6
by: aznimah | last post by:
hi, i'm work on image comparison. i'm using the similarity measurement which i need to: 1) convert the image into the binary form since the algorithm that i've use works with binary data for the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.