Wednesday, March 28, 2012

String.Compare vs String.CompareOrdinal

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,
--
JoeHi 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.
"Joe" <Joe@.discussions.microsoft.com> wrote in message
news:15BEF25B-EFE7-4D97-99A9-2D8A364C0EE3@.microsoft.com...
> 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,
> --
> Joe
Thank you. That was very helpful!
--
Joe
"Kevin Spencer" wrote:

> 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.
> "Joe" <Joe@.discussions.microsoft.com> wrote in message
> news:15BEF25B-EFE7-4D97-99A9-2D8A364C0EE3@.microsoft.com...
>
>

0 comments:

Post a Comment