473,386 Members | 1,819 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,386 software developers and data experts.

Comparing strings

I was comparing strings, as one does:

stringA.CompareTo(stringB) == -1

Then I thought that this was not as easy to read as it could be,
wouldn't it be clearer to write:

stringA < stringB

Of course that doesn't compile since the < operator is not defined
between strings. Easy enough I thought, I can write the < operator:

static public bool operator <(string a, string b)
{ return a.CompareTo(b) == -1; }

of course I also need the > operator as well:

static public bool operator >(string a, string b)
{ return a.CompareTo(b) == +1; }

The problem is that neither of these will compile since neither
parameter is of the enclosing class and they have to be inside a
class.

My next idea was to derive a new string class which added the two
operators. No joy, String is sealed so I can't derive from it.

I could write a whole string class of my own from scratch but that
seems to be overkill for this problem. Is there any other solution
out there?

Thanks,

rossum


The ultimate truth is that there is no ultimate truth
Nov 17 '05 #1
4 1899
rossum <ro******@coldmail.com> wrote:
I was comparing strings, as one does:

stringA.CompareTo(stringB) == -1


Just one point - you shouldn't compare to -1. You should see whether
the return value is negative. There's no guarantee that
String.CompareTo(String) will compare -1/0/1.

I'd just keep it as String.CompareTo myself - do you compare strings
(other than for equality/inequality) that often? It's something I
rarely do, personally, but obviously everyone does different things.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
I can offer no solution to the problem you discuss, but to make the
proble a bit less onerous:

Instead of :
stringA.CompareTo(stringB) == -1
use
stringA.CompareTo(stringB) < 0

The advantage is that in all cases: (op: ==, <, > )
stringA.CompareTo(stringB) op 0
is effectively the same as:
stringA op stringB

So, it requires fewer contortions to mentally convert what you had to write
into what you expect to see.

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"rossum" <ro******@coldmail.com> wrote in message
news:am********************************@4ax.com...
I was comparing strings, as one does:

stringA.CompareTo(stringB) == -1

Then I thought that this was not as easy to read as it could be,
wouldn't it be clearer to write:

stringA < stringB

Of course that doesn't compile since the < operator is not defined
between strings. Easy enough I thought, I can write the < operator:

static public bool operator <(string a, string b)
{ return a.CompareTo(b) == -1; }

of course I also need the > operator as well:

static public bool operator >(string a, string b)
{ return a.CompareTo(b) == +1; }

The problem is that neither of these will compile since neither
parameter is of the enclosing class and they have to be inside a
class.

My next idea was to derive a new string class which added the two
operators. No joy, String is sealed so I can't derive from it.

I could write a whole string class of my own from scratch but that
seems to be overkill for this problem. Is there any other solution
out there?

Thanks,

rossum


The ultimate truth is that there is no ultimate truth

Nov 17 '05 #3
On Fri, 24 Jun 2005 16:54:38 -0400, "James Curran"
<ja*********@mvps.org> wrote:
I can offer no solution to the problem you discuss, but to make the
proble a bit less onerous:

Instead of :
stringA.CompareTo(stringB) == -1
use
stringA.CompareTo(stringB) < 0

The advantage is that in all cases: (op: ==, <, > )
stringA.CompareTo(stringB) op 0
is effectively the same as:
stringA op stringB

So, it requires fewer contortions to mentally convert what you had to write
into what you expect to see.

--


Thanks James. It is still a pity that <, > etc. are not defined for
strings. Coming from C++ I am used to being able to define pretty
much any operator that I want.

rossum

The ultimate truth is that there is no ultimate truth
Nov 17 '05 #4
Hmm. There is String.Compare(strA, strB) < 0;

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #5

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

Similar topics

5
by: beliavsky | last post by:
By mistake I coded something like print ("1" > 1) and got the result "True". Comparing an integer and a string seems meaningless to me, and I would prefer to have an exception thrown. Can...
26
by: William Park | last post by:
How do you compare 2 strings, and determine how much they are "close" to each other? Eg. aqwerty qwertyb are similar to each other, except for first/last char. But, how do I quantify that? ...
5
by: Curtis Gilchrist | last post by:
I am required to read in records from a file and store them in descending order by an customer number, which is a c-style string of length 5. I am storing these records in a linked list. My...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
6
by: BrianJones | last post by:
I have a problem with the int strcmp(str1,str2) function: When I do: char *pass; char *passv; pass = getpass("Please enter....."); passv = getpass("Please verify.....");
88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
2
by: Manny Chohan | last post by:
Hi, i have two datetime values in format 11/22/04 9:00 AM and 11/22/04 9:30 AM. How can i compare dates .net c# or if there is any other way such as Javascript. Thanks Manny
15
by: luc.saffre | last post by:
Hello, here is something that surprises me. #coding: iso-8859-1 s1=u"Frau Müller machte große Augen" s2="Frau Müller machte große Augen" if s1 == s2: pass
1
by: Jetboy555 | last post by:
Sample input: 2000 Georgia Tech 30 Virginia 20 1999 Virginia 20 Virginia tech My Problem is in taking the input in correctly. I take the year in correctly, but i'm having trouble with the...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.