text
stringlengths
0
13.4k
A small optimization may rewrite EnumerateCharacters() extension method specifically for this purpose:
public static class StringExtensions
{
public static int CountCharacters(this string text)
{
if (String.IsNullOrEmpty(text))
return 0;
int count = 0;
var enumerator = StringInfo.GetTextElementEnumerator(text);
while (enumerator.MoveNext())
++count;
return count;
}
}
Section 2.2: Count distinct characters
If you need to count distinct characters then, for the reasons explained in Remarks section, you can't simply use
Length property because it's the length of the array of System.Char which are not characters but code-units (not
Unicode code-points nor graphemes). If, for example, you simply write text.Distinct().Count() you will get
incorrect results, correct code:
int distinctCharactersCount = text.EnumerateCharacters().Count();
One step further is to count occurrences of each character, if performance aren't an issue you may simply do it
like this (in this example regardless of case):
var frequencies = text.EnumerateCharacters()
.GroupBy(x => x, StringComparer.CurrentCultureIgnoreCase)
.Select(x => new { Character = x.Key, Count = x.Count() };
Section 2.3: Convert string to/from another encoding
.NET strings contain System.Char (UTF-16 code-units). If you want to save (or manage) text with another encoding
you have to work with an array of System.Byte.
Conversions are performed by classes derived from System.Text.Encoder and System.Text.Decoder which,
together, can convert to/from another encoding (from a byte X encoded array byte[] to an UTF-16 encoded
System.String and vice-versa).
Because the encoder/decoder usually works very close to each other they're grouped together in a class derived
GoalKicker.com – .NET Framework Notes for Professionals
5
from System.Text.Encoding, derived classes offer conversions to/from popular encodings (UTF-8, UTF-16 and so
on).
Examples:
Convert a string to UTF-8
byte[] data = Encoding.UTF8.GetBytes("This is my text");
Convert UTF-8 data to a string
var text = Encoding.UTF8.GetString(data);
Change encoding of an existing text file
This code will read content of an UTF-8 encoded text file and save it back encoded as UTF-16. Note that this code is
not optimal if file is big because it will read all its content into memory:
var content = File.ReadAllText(path, Encoding.UTF8);
File.WriteAllText(content, Encoding.UTF16);
Section 2.4: Comparing strings
Despite String is a reference type == operator compares string values rather than references.
As you may know string is just an array of characters. But if you think that strings equality check and comparison
is made character by character, you are mistaken. This operation is culture specific (see Remarks below): some
character sequences can be treated as equal depending on the culture.
Think twice before short circuiting equality check by comparing Length properties of two strings!
Use overloads of String.Equals method which accept additional StringComparison enumeration value, if you
need to change default behavior.
Section 2.5: Count occurrences of a character
Because of the reasons explained in Remarks section you can't simply do this (unless you want to count occurrences
of a specific code-unit):
int count = text.Count(x => x == ch);