text stringlengths 0 13.4k |
|---|
You need a more complex function: |
public static int CountOccurrencesOf(this string text, string character) |
{ |
return text.EnumerateCharacters() |
.Count(x => String.Equals(x, character, StringComparer.CurrentCulture)); |
} |
Note that string comparison (in contrast to character comparison which is culture invariant) must always be |
performed according to rules to a specific culture. |
Section 2.6: Split string into fixed length blocks |
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 |
GoalKicker.com – .NET Framework Notes for Professionals |
6 |
number of graphemes not the number of code-units): |
public static IEnumerable<string> Split(this string value, int desiredLength) |
{ |
var characters = StringInfo.GetTextElementEnumerator(value); |
while (characters.MoveNext()) |
yield return String.Concat(Take(characters, desiredLength)); |
} |
private static IEnumerable<string> Take(TextElementEnumerator enumerator, int count) |
{ |
for (int i = 0; i < count; ++i) |
{ |
yield return (string)enumerator.Current; |
if (!enumerator.MoveNext()) |
yield break; |
} |
} |
Section 2.7: Object.ToString() virtual method |
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 implicitly called when concatinating value with a string: |
public class Foo |
{ |
public override string ToString() |
{ |
return "I am Foo"; |
} |
} |
var foo = new Foo(); |
Console.WriteLine("I am bar and "+foo);// outputs I am bar and I am Foo |
The result of this method is also extensively used by debugging tools. If, for some reason, you do not want to |
override this method, but want to customize how debugger shows the value of your type, use DebuggerDisplay |
Attribute (MSDN): |
// [DebuggerDisplay("Person = FN {FirstName}, LN {LastName}")] |
[DebuggerDisplay("Person = FN {"+nameof(Person.FirstName)+"}, LN {"+nameof(Person.LastName)+"}")] |
public class Person |
{ |
public string FirstName { get; set; } |
public string LastName { get; set;} |
// ... |
} |
GoalKicker.com – .NET Framework Notes for Professionals |
7 |
Section 2.8: Immutability of strings |
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 = ... |
// memory is allocated |
string newString = veryLongString.Remove(0,1); // removes first character of the string. |
If you need to perform many operations with string value, use StringBuilder class which is designed for efficient |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.