Tutorial by Examples

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 tex...
If you need to count 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). Correct code is then: int length = text.Enume...
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); You need a more complex function: public static int CountOccurrencesOf(this string text, string character) { ...
We cannot break a string into arbitrary points (because a System.Char may not be valid alone because it's a combining character or part of a surrogate) then code must take that into account (note that with length I mean the number of graphemes not the number of code-units): public static IEnumerabl...
.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 ...
Everything in .NET is an object, hence every type has ToString() method defined in Object class which can be overridden. Default implementation of this method just returns the name of the type: public class Foo { } var foo = new Foo(); Console.WriteLine(foo); // outputs Foo ToString() is i...
Strings are immutable. You just cannot change existing string. Any operation on the string crates a new instance of the string having new value. It means that if you need to replace a single character in a very long string, memory will be allocated for a new value. string veryLongString = ... // m...
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 ...

Page 1 of 1