Monday, 26 November 2012
c# for dummies: c sharp programming
c# for dummies: c sharp programming: String equality comparison Despite ordinal’s limitations, string’s == operator always performs ordinal case- sensitive comparison. The...
c sharp programming
String equality comparison
Despite ordinal’s limitations, string’s == operator always performs ordinal case-
sensitive comparison. The same goes for the instance version of string.Equals when
called without arguments; this defines the “default” equality comparison behavior
for the string type.
The ordinal algorithm was chosen for string’s == and Equals
functions because it’s both highly efficient and deterministic.
String equality comparison is considered fundamental and is
performed far more frequently than order comparison.
A “strict” notion of equality is also consistent with the general
use of the == operator.
The following methods allow culture-aware or case-insensitive comparisons:
public bool Equals(string value, StringComparison comparisonType);
public static bool Equals (string a, string b,
StringComparison comparisonType);
The static version is advantageous in that it still works if one or both of the strings
are null. StringComparison is an enum defined as follows:
The static version is advantageous in that it still works if one or both of the strings
are null. StringComparison is an enum defined as follows:
public enum StringComparison
{
CurrentCulture, // Case-sensitive
CurrentCultureIgnoreCase,
InvariantCulture, // Case-sensitive
InvariantCultureIgnoreCase,
Ordinal, // Case-sensitive
OrdinalIgnoreCase
}
For example:
Console.WriteLine (string.Equals ("foo", "FOO",
StringComparison.OrdinalIgnoreCase)); // True
Console.WriteLine ("?" == "u"); // False
Console.WriteLine (string.Equals ("?", "u",
StringComparison.CurrentCulture)); // ?
(The result of the final comparison is determined by the computer’s current language
settings.)
Despite ordinal’s limitations, string’s == operator always performs ordinal case-
sensitive comparison. The same goes for the instance version of string.Equals when
called without arguments; this defines the “default” equality comparison behavior
for the string type.
The ordinal algorithm was chosen for string’s == and Equals
functions because it’s both highly efficient and deterministic.
String equality comparison is considered fundamental and is
performed far more frequently than order comparison.
A “strict” notion of equality is also consistent with the general
use of the == operator.
The following methods allow culture-aware or case-insensitive comparisons:
public bool Equals(string value, StringComparison comparisonType);
public static bool Equals (string a, string b,
StringComparison comparisonType);
The static version is advantageous in that it still works if one or both of the strings
are null. StringComparison is an enum defined as follows:
The static version is advantageous in that it still works if one or both of the strings
are null. StringComparison is an enum defined as follows:
public enum StringComparison
{
CurrentCulture, // Case-sensitive
CurrentCultureIgnoreCase,
InvariantCulture, // Case-sensitive
InvariantCultureIgnoreCase,
Ordinal, // Case-sensitive
OrdinalIgnoreCase
}
For example:
Console.WriteLine (string.Equals ("foo", "FOO",
StringComparison.OrdinalIgnoreCase)); // True
Console.WriteLine ("?" == "u"); // False
Console.WriteLine (string.Equals ("?", "u",
StringComparison.CurrentCulture)); // ?
(The result of the final comparison is determined by the computer’s current language
settings.)
c# for dummies: StringBuilder in c#
c# for dummies: StringBuilder in c#: StringBuilder The StringBuilder class (System.Text namespace) represents a mutable (editable) string. With a StringBuilder, you can Append, ...
StringBuilder in c#
StringBuilder
The StringBuilder class (System.Text namespace) represents a mutable (editable)
string. With a StringBuilder, you can Append, Insert, Remove, and Replace substrings
without replacing the whole StringBuilder.
StringBuilder’s constructor optionally accepts an initial string value, as well as a
starting size for its internal capacity (default is 16 characters). If you go above this,
StringBuilder automatically resizes its internal structures to accommodate (at a
slight performance cost) up to its maximum capacity (default is int.MaxValue).
A popular use of StringBuilder is to build up a long string by repeatedly calling
Append. This approach is much more efficient than repeatedly concatenating ordi-
nary string types:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 50; i++) sb.Append (i + ",");
To get the final result, call ToString():
Console.WriteLine (sb.ToString());
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,
27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,
In our example, the expression i + "," means that we’re still
repeatedly concatenating strings. However, this incurs only a
small performance cost in that the strings in question are small
and don’t grow with each loop iteration. For maximum per-
formance, however, we could change the loop body to this:
{ sb.Append (i.ToString()); sb.Append (","); }
AppendLine performs an Append that adds a new line sequence ("\r\n" in Windows).
AppendFormat accepts a composite format string, just like String.Format.
As well as the Insert, Remove, and Replace methods (Replace functions such as
string’s Replace), StringBuilder defines a Length property and a writable indexer for
getting/setting individual characters.
To clear the contents of a StringBuilder, either instantiate a new one or set its
Length to zero.
Setting a StringBuilder’s Length to zero doesn’t shrink its in-
ternal capacity. So, if the StringBuilder previously contained 1
million characters, it will continue to occupy around 2 MB of
memory after zeroing its Length. If you want to release the mem-
ory, you must create a new StringBuilder and allow the old one
to drop out of scope (and be garbage-collected).
The StringBuilder class (System.Text namespace) represents a mutable (editable)
string. With a StringBuilder, you can Append, Insert, Remove, and Replace substrings
without replacing the whole StringBuilder.
StringBuilder’s constructor optionally accepts an initial string value, as well as a
starting size for its internal capacity (default is 16 characters). If you go above this,
StringBuilder automatically resizes its internal structures to accommodate (at a
slight performance cost) up to its maximum capacity (default is int.MaxValue).
A popular use of StringBuilder is to build up a long string by repeatedly calling
Append. This approach is much more efficient than repeatedly concatenating ordi-
nary string types:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 50; i++) sb.Append (i + ",");
To get the final result, call ToString():
Console.WriteLine (sb.ToString());
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,
27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,
In our example, the expression i + "," means that we’re still
repeatedly concatenating strings. However, this incurs only a
small performance cost in that the strings in question are small
and don’t grow with each loop iteration. For maximum per-
formance, however, we could change the loop body to this:
{ sb.Append (i.ToString()); sb.Append (","); }
AppendLine performs an Append that adds a new line sequence ("\r\n" in Windows).
AppendFormat accepts a composite format string, just like String.Format.
As well as the Insert, Remove, and Replace methods (Replace functions such as
string’s Replace), StringBuilder defines a Length property and a writable indexer for
getting/setting individual characters.
To clear the contents of a StringBuilder, either instantiate a new one or set its
Length to zero.
Setting a StringBuilder’s Length to zero doesn’t shrink its in-
ternal capacity. So, if the StringBuilder previously contained 1
million characters, it will continue to occupy around 2 MB of
memory after zeroing its Length. If you want to release the mem-
ory, you must create a new StringBuilder and allow the old one
to drop out of scope (and be garbage-collected).
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);
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 ...
Subscribe to:
Posts (Atom)