Comparing strings

  • Thread starter Thread starter rossum
  • Start date Start date
R

rossum

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
 
rossum said:
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.
 
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top