By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,330 Members | 1,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,330 IT Pros & Developers. It's quick & easy.

Case Insensitive String Comparison?

P: n/a
All:

The String class has a CompareTo method, that compares to strings. Is there
a similar method that performs case-insensitive comparisons?

Thanks,
John
Feb 1 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
jpuopolo <jp******@hotmail.comwrote:
The String class has a CompareTo method, that compares to strings. Is there
a similar method that performs case-insensitive comparisons?
In 2.0 there are overloads of Compare which take a StringComparison,
allowing you to specify case- or culture-insensitive comparisons.

--
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
Feb 1 '07 #2

P: n/a
See String.Compare(str1, str2, true)

Boaz Ben-Porat
Milestone Systems

"jpuopolo" <jp******@hotmail.comskrev i en meddelelse
news:%2***************@TK2MSFTNGP04.phx.gbl...
All:

The String class has a CompareTo method, that compares to strings. Is
there a similar method that performs case-insensitive comparisons?

Thanks,
John


Feb 1 '07 #3

P: n/a
"jpuopolo" <jp******@hotmail.comwrote in
news:#U*************@TK2MSFTNGP04.phx.gbl:

The String class has a CompareTo method, that compares to strings. Is
there a similar method that performs case-insensitive comparisons?
If you can't use the other suggestions you could try to convert both
strings to upper or lower case before comparing them.
Feb 2 '07 #4

P: n/a
On Feb 2, 8:01 am, Peter K <xdz...@hotmail.comwrote:
"jpuopolo" <jpuop...@hotmail.comwrote innews:#U*************@TK2MSFTNGP04.phx.gbl:
The String class has a CompareTo method, that compares to strings. Is
there a similar method that performs case-insensitive comparisons?

If you can't use the other suggestions you could try to convert both
strings to upper or lower case before comparing them.
You need to be *very* careful with that, and consider which culture
you want to use when upper/lower-casing. You might think that
"mail".ToUpper() will give "MAIL" - but it doesn't in Turkey...

Jon

Feb 2 '07 #5

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in
news:11*********************@p10g2000cwp.googlegro ups.com:
On Feb 2, 8:01 am, Peter K <xdz...@hotmail.comwrote:
>"jpuopolo" <jpuop...@hotmail.comwrote
innews:#U*************@TK2MSFTNGP04.phx.gbl:
The String class has a CompareTo method, that compares to strings.
Is there a similar method that performs case-insensitive
comparisons?

If you can't use the other suggestions you could try to convert both
strings to upper or lower case before comparing them.

You need to be *very* careful with that, and consider which culture
you want to use when upper/lower-casing. You might think that
"mail".ToUpper() will give "MAIL" - but it doesn't in Turkey...
Thanks. My programs have always either been with English or Danish texts so
I've never seen that problem with ToUpper or ToLower. What do you do when
you use ToUpper then - always ensure that the culture is set to something
you know so you know what "uppercase" means?
Feb 2 '07 #6

P: n/a
On Feb 1, 1:43 pm, "jpuopolo" <jpuop...@hotmail.comwrote:
All:

The String class has a CompareTo method, that compares to strings. Is there
a similar method that performs case-insensitive comparisons?

Thanks,
John

I used String.Compare() and it worked well.

Thanks, all....

Feb 2 '07 #7

P: n/a
Peter K <xd****@hotmail.comwrote:
You need to be *very* careful with that, and consider which culture
you want to use when upper/lower-casing. You might think that
"mail".ToUpper() will give "MAIL" - but it doesn't in Turkey...

Thanks. My programs have always either been with English or Danish texts so
I've never seen that problem with ToUpper or ToLower. What do you do when
you use ToUpper then - always ensure that the culture is set to something
you know so you know what "uppercase" means?
If you're using it for case-insensitive comparisons, you should specify
CultureInfo.InvariantCulture, or use ToUpperInvariant or
ToLowerInvariant.

--
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
Feb 2 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.