Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Comparing Strings

0 views
Skip to first unread message

Jonathan Wood

unread,
Jan 26, 2006, 3:12:53 PM1/26/06
to
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.

--
Jonathan Wood
SoftCircuits
http://www.softcircuits.com


Naveen

unread,
Jan 26, 2006, 3:27:33 PM1/26/06
to

Ignacio Machin ( .NET/ C# MVP )

unread,
Jan 26, 2006, 3:29:35 PM1/26/06
to
Hi,


"Jonathan Wood" <jw...@softcircuits.com> wrote in message
news:u5553TrI...@TK2MSFTNGP15.phx.gbl...


> 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


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Jon Skeet [C# MVP]

unread,
Jan 26, 2006, 3:38:41 PM1/26/06
to
Jonathan Wood <jw...@softcircuits.com> wrote:
> 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>

--
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

Jonathan Wood

unread,
Jan 26, 2006, 5:48:37 PM1/26/06
to
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/default.asp?url=/library/en-us/cpref/html/frlrfSystemCollectionsArrayListClassBinarySearchTopic.asp

--
Jonathan Wood
SoftCircuits
http://www.softcircuits.com

Available for consulting: http://www.softcircuits.com/jwood/resume.htm


Jon Skeet [C# MVP]

unread,
Jan 26, 2006, 5:59:36 PM1/26/06
to
Jonathan Wood <jw...@softcircuits.com> wrote:
> > 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)?

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.

> > 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/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.

Jonathan Wood

unread,
Jan 26, 2006, 8:58:00 PM1/26/06
to
Jon,

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

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

Thanks.

Jeff Louie

unread,
Jan 26, 2006, 10:39:28 PM1/26/06
to
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

*** Sent via Developersdex http://www.developersdex.com ***

0 new messages