String.Compare vs String.CompareOrdinal

G

Guest

Hello All:

Why would you use String.Compare in lieu of String.CompareOrdinal? For that
matter, why not use string1.CompareTo(string2)?

Are there performance differences?

Any feedback would be appreciated.

TIA,
 
K

Kevin Spencer

Hi Joe,

String.Compare can be or not be case-sensitive, and can incorporate any
number of comparison options, including culture-sensitivity and sorting
rules. That is, it is not comparing the exact strings necessarily, but can
be configured to do various sorts of comparisons. String.CompareOrdinal
compares the numeric values of the individual Unicode characters of the
strings. In other words, it is always case-sensitive, and never
culture-sensitive.

String.CompareOrdinal is faster (more efficient) when you don't need to do a
culture-sensitive comparison, and you do want to do a case-sensitive
comparison.

String.CompareTo is case-sensitive, using an Ordinal comparison, and
culture-sensitive, but always uses the current culture. It is faster than
String.Compare, but less efficient than String.CompareOrdinal.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.
 
G

Guest

Thank you. That was very helpful!
--
Joe


Kevin Spencer said:
Hi Joe,

String.Compare can be or not be case-sensitive, and can incorporate any
number of comparison options, including culture-sensitivity and sorting
rules. That is, it is not comparing the exact strings necessarily, but can
be configured to do various sorts of comparisons. String.CompareOrdinal
compares the numeric values of the individual Unicode characters of the
strings. In other words, it is always case-sensitive, and never
culture-sensitive.

String.CompareOrdinal is faster (more efficient) when you don't need to do a
culture-sensitive comparison, and you do want to do a case-sensitive
comparison.

String.CompareTo is case-sensitive, using an Ordinal comparison, and
culture-sensitive, but always uses the current culture. It is faster than
String.Compare, but less efficient than String.CompareOrdinal.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.
 

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