Case Insensitive string comparison

  • Thread starter Thread starter anonieko
  • Start date Start date
A

anonieko

Is this a long way to compare strings ignoring the case?


if ( string.Compare(wsUri.Host, "localHost",
System.StringComparison.OrdinalIgnoreCase) == 0 )
{
// etc.
}
 
Is this a long way to compare strings ignoring the case?


if ( string.Compare(wsUri.Host, "localHost",
System.StringComparison.OrdinalIgnoreCase) == 0 )
{
// etc.
}

Well, it's easier to use

if ("localHost".Equals(wsUri.Host, StringComparison.OrdinalIgnoreCase))

(If you know that wsUri.Host is non-null, you can swap what the method
is called on for clarity.)
 
Well it's long, but just as number of characters.
A shorter version could be string.Compare(wsUri.Host, "localHost",true)==0
It still takes three parameters.
 
Well it's long, but just as number of characters.
A shorter version could be string.Compare(wsUri.Host, "localHost",true)==0
It still takes three parameters.

<[email protected]> ha scritto nel messaggio




- Show quoted text -

wsUri.Host.ToString().ToUpper().Equals("LOCALHOST") would be much
simpler i think

-
shashank kadge
 
wsUri.Host.ToString().ToUpper().Equals("LOCALHOST") would be much
simpler i think

It's not guaranteed to work though, due to potential culture issues.

I ran into a problem years ago where I was trying to compare strings
in a case-insensitive way by doing the above - and we found that in
Turkey, we had a problem, because the upper case of "i" isn't "I" in
Turkey (and vice versa).

Using a case-insensitive comparison is the best way of working.

Jon
 
It's not guaranteed to work though, due to potential culture issues.

I ran into a problem years ago where I was trying to compare strings
in a case-insensitive way by doing the above - and we found that in
Turkey, we had a problem, because the upper case of "i" isn't "I" in
Turkey (and vice versa).

Using a case-insensitive comparison is the best way of working.

Jon

Got u Jon. thanks.

-
shashank kadge
 
A nice shorthand overload is:

String.Compare(wsUri.Host, "localHost", true) == 0 ...

--
HTH,

Kevin Spencer
Microsoft MVP

Help test our new betas,
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
Back
Top