Monday, 26 November 2012

c# for dummies: c sharp programming

c# for dummies: c sharp programming: String order comparison String’s CompareTo instance method performs culture-sensitive, case-sensitive order comparison. Unlike the == operat...

c sharp programming

String order comparison



String’s CompareTo instance method performs culture-sensitive, case-sensitive order
comparison. Unlike the == operator, CompareTo does not use ordinal comparison: for
ordering, a culture-sensitive algorithm is much more useful.
Here’s the method’s definition:
public int CompareTo (string strB);
The  CompareTo  instance  method  implements  the  generic
IComparable  interface,  a  standard  comparison  protocol  used
across  the  .NET  Framework. This means  string’s  CompareTo
defines the default ordering behavior strings,  in such applica-
tions as sorted collections, for instance. For more information


For other kinds of comparison, you can call the static Compare and CompareOrdinal
methods:
public static int Compare (string strA, string strB,
                           StringComparison comparisonType);
public static int Compare (string strA, string strB, bool ignoreCase,
                           CultureInfo culture);
public static int Compare (string strA, string strB, bool ignoreCase);
public static int CompareOrdinal (string strA, string strB);
The last two methods are simply shortcuts for calling the first two methods.
All of the order comparison methods return a positive number, a negative number,
or zero, depending on whether the first value comes after, before, or alongside the
second value:
Console.WriteLine ("Boston".CompareTo ("Austin"));    // 1
Console.WriteLine ("Boston".CompareTo ("Boston"));    // 0
Console.WriteLine ("Boston".CompareTo ("Chicago"));   // -1
Console.WriteLine ("?".CompareTo ("u"));              // 0
Console.WriteLine ("foo".CompareTo ("FOO"));          // -1


The following performs a case-insensitive comparison using the current culture:
Console.WriteLine (string.Compare ("foo", "FOO", true));   // 0
By supplying a CultureInfo object, you can plug in any alphabet:
// CultureInfo is defined in the System.Globalization namespace
CultureInfo german = CultureInfo.GetCultureInfo ("de-DE");
int i = string.Compare ("Müller", "Muller", false, german);

c# for dummies: c sharp programming

c# for dummies: c sharp programming: Ordinal versus culture comparison There are two basic algorithms for string comparison: ordinal and culture-sensitive. Ordinal  comparisons ...

c sharp programming

Ordinal versus culture comparison



There are two basic algorithms for string comparison: ordinal and culture-sensitive.
Ordinal  comparisons  interpret  characters  simply  as numbers  (according  to  their
numeric Unicode  value);  culture-sensitive  comparisons  interpret  characters with
reference to a particular alphabet. There are two special cultures: the “current cul-
ture,” which is based on settings picked up from the computer’s control panel, and
the “invariant culture,” which  is  the  same on every computer  (and closely maps
American culture).
For equality comparison, both ordinal and culture-specific algorithms are useful.
For ordering, however, culture-specific comparison is nearly always preferable: to
order strings alphabetically, you need an alphabet. Ordinal relies on the numeric
Unicode  point  values,  which  happen  to  put  English  characters  in  alphabetical


order—but even then not exactly as you might expect. For example, assuming case-
sensitivity, consider the strings “Atom”, “atom”, and “Zamia”. The invariant culture
puts them in the following order:
"Atom", "atom", "Zamia"
Ordinal arranges them instead as follows:
"Atom", "Zamia", "atom"
This is because the invariant culture encapsulates an alphabet, which considers up-
percase characters adjacent to their lowercase counterparts (AaBbCcDd…). The or-
dinal  algorithm,  however,  puts  all  the  uppercase  characters  first,  and  then  all
lowercase characters (A..Z, a..z). This is essentially a throwback to the ASCII char-
acter set invented in the 1960s.

c# for dummies: Comparing Strings in c#

c# for dummies: Comparing Strings in c#: Comparing Strings In comparing two values, the .NET Framework differentiates the concepts of equal- ity comparison and order comparison. Equ...

Comparing Strings in c#

Comparing Strings


In comparing two values, the .NET Framework differentiates the concepts of equal-
ity comparison and order comparison. Equality comparison  tests whether  two  in-
stances  are  semantically  the  same; order  comparison  tests which of  two  (if  any)
instances comes first when arranging them in ascending or descending sequence.
Equality comparison  is not a subset of order comparison;  the
two systems have different purposes. It’s legal, for instance, to
have two unequal values in the same ordering position. We re-
sume this topic in “Equality Comparison” on page 245.
For  string  equality  comparison,  you  can  use  the  ==  operator  or  one  of  string’s
Equals methods. The  latter  are more  versatile because  they  allow  you  to  specify
options such as case-insensitivity.
Another difference is that == does not work reliably on strings
if the variables are cast to the object type.


For string order comparison, you can use either the CompareTo instance method or
the static Compare and CompareOrdinal methods: these return a positive or negative
number—or  zero—depending on whether  the  first  value  comes before,  after, or
alongside the second.
Before going into the details of each, we need to examine .NET’s underlying string
comparison algorithms.

c# for dummies: c# for dummies

c# for dummies: c# for dummies: String.Format and composite format strings The static Format method provides a convenient way  to build strings  that embed variables.  The ...