473,835 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.CompareO rdinal ??????

HELP!
Jan 23 '06 #1
6 1984
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.CompareO rdinal ??????


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

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.CompareO rdinal ??????

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**@discussio ns.microsoft.co m> 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.CompareO rdinal ??????

HELP!


bceggersATcomca stDOTnet
Jan 26 '06 #4
Barry <bc******@castc om.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.WriteLi ne ("A+".CompareTo ("A-"))
Console.WriteLi ne ("A+A".CompareT o("A-A"))
End Sub

End Class

That prints out 1 then -1.

--
Jon Skeet - <sk***@pobox.co m>
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.co m> wrote:
Barry <bc******@castc om.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.WriteLi ne ("A+".CompareTo ("A-"))
Console.WriteLi ne ("A+A".CompareT o("A-A"))
End Sub

End Class

That prints out 1 then -1.


bceggersATcomca stDOTnet
Jan 27 '06 #6
Barry <bc******@castc om.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.co m>
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
1698
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 below. Does anyone know an easier way to accomplish this? Essentially, I want to avoid doing an 'eval' on a string to get it back into dict form... but still allow nested structures. (My current code doesn't handle nested structures.) I conked...
6
4556
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 am using this code to compare the file lists: def compare_files(first_list, second_list, first_dir, second_dir): missing = in_first_only(first_list, second_list) for item in missing: index = first_list.index(item)
0
2924
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 C++ version 3.x), but perhaps it's more general than that. The following program (to be compiled with g++ 3.x, 2.9x is not enough) #include <iostream> #include <string>
3
2317
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 <vector> 5
19
9536
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 have tried to print out by debug.writeline(). But the "==" operator results false, and string.Compare() results true. Somebody helps me!
10
14492
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 the "right" (i.e.wysiwyg) answer while compare() does not.
33
4699
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 of articles I read from different experts and programmers tell me that their "gut feelings" for using stringBuilder instead of string concatenation is when the number of string concatunation is more then N ( N varies between 3 to max 15 from...
232
13402
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 set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
6
4354
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 computation 2) compare the string binary data to get the similarity or dissimilarity result. The problem is, i already done with the image (jpg) conversion to binary and also try the algorithm structure in C# language, but i having a problem to...
0
10815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10526
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10563
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9348
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7770
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6970
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5808
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4435
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3999
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.