Comparing Strings

J

Jonathan Wood

Can someone tell me how to compare two strings using a case-insensitive
comparison.

Ideally, I would prefer not to have to change a global setting or override
any other interfaces.

BTW, does anyone know why documentation for .NET library methods don't have
a Return Value section where the return value is described? Seems really
strange to me that Microsoft didn't feel return values needed to be
documented.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


Jonathan Wood said:
Can someone tell me how to compare two strings using a case-insensitive
comparison.

Ideally, I would prefer not to have to change a global setting or override
any other interfaces.
Hi, IIRC in v 2.0 there is a new recommended way to handle string
comparisions
Ok, I found it in google :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/StringsinNET20.asp


BTW, does anyone know why documentation for .NET library methods don't
have a Return Value section where the return value is described? Seems
really strange to me that Microsoft didn't feel return values needed to be
documented.

Hi,

In general you do have it, only in some cases you do not have that section,
note that this is only for methods (not properties) that return something
 
J

Jon Skeet [C# MVP]

Jonathan Wood said:
Can someone tell me how to compare two strings using a case-insensitive
comparison.

In 2.0, use String.Compare (first, second,
StringComparison.OrdinalIgnoreCase)

(Or CurrentCultureIgnoreCase, or InvariantCultureIgnoreCase)

In 1.1, use String.Compare (first, second, true) if you're happy using
the current culture.
Ideally, I would prefer not to have to change a global setting or override
any other interfaces.

BTW, does anyone know why documentation for .NET library methods don't have
a Return Value section where the return value is described? Seems really
strange to me that Microsoft didn't feel return values needed to be
documented.

It does. In the case of String.Compare(string,string,StringComparison)
it's:

<quote>
Return Value
A 32-bit signed integer indicating the lexical relationship between the
two comparands.
Value
Condition

Less than zero
strA is less than strB.

Zero
strA equals strB.

Greater than zero
strA is greater than strB.
</quote>
 
J

Jonathan Wood

Hi Jon,
In 1.1, use String.Compare (first, second, true) if you're happy using
the current culture.

Ack! Okay, I see that now.

Can someone explain to me why we're using this syntax? I guess Compare is a
static function. But why is it not part of the string class? (i.e., string
s; s.Compare(first, second, true) or s.Compare(second, true)?

Thanks.
It does. In the case of String.Compare(string,string,StringComparison)
it's:

A few do. Many do not. For example:
http://msdn.microsoft.com/library/d...ollectionsArrayListClassBinarySearchTopic.asp
 
J

Jon Skeet [C# MVP]

Jonathan Wood said:
Ack! Okay, I see that now.

Can someone explain to me why we're using this syntax? I guess Compare is a
static function. But why is it not part of the string class? (i.e., string
s; s.Compare(first, second, true) or s.Compare(second, true)?

Well, having it as a static method means it's available without you
having to worry about whether or not either side is null. Yes, each of
the overloads *could* be available as an instance method too, but I'm
not sure it would give very much benefit.
A few do. Many do not. For example:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpre
f/html/frlrfSystemCollectionsArrayListClassBinarySearchTopic.asp

Every one of those overloads has a "Return value" section. It's only
the page listing the overloads which doesn't.
 
J

Jonathan Wood

Jon,
Well, having it as a static method means it's available without you
having to worry about whether or not either side is null. Yes, each of
the overloads *could* be available as an instance method too, but I'm
not sure it would give very much benefit.

I guess. It's just seems less OOP to me this way.

Thanks.
 
J

Jeff Louie

It is a less "object oriented style of computation", but x.is_equal(y)
failed in the
real world spawning the equal(x,y) convention. pp275
OOSoftwareConstruction
ed.2 Meyer.

Regards,
Jeff
 

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

Top