HomeSearch

C# String

Explore string methods: create, search and change strings with built-in code.
String. You look into a reflecting pool. A message suddenly appears. It is a string written upon the water's surface. Strings have words and characters.
String methods. Usually strings do not appear in reflecting pools. But we can manipulate any C# string with methods. New strings are returned.CompareCompareOrdinalCompareToConcatContainsCopyCopyToEndsWithEmptyEqualsFormatGetEnumeratorGetHashCodeIndexOfIndexOfAnyInsertInternIsInternedIsNormalizedIsNullOrEmptyIsNullOrWhiteSpaceJoinLastIndexOfLastIndexOfAnyLengthNormalizePadLeftPadRightRemoveReplaceSplitStartsWithSubstringToCharArrayToLowerToLowerInvariantToStringToUpperToUpperInvariantTrimTrimEndTrimStart
String interpolation. A string can be created with interpolated values in it. This is similar to string.Format but the syntax is different. We precede the string literal with a "$" sign.String Interpolation
Constructor. Strings are often reused, and passed around within a program. Methods like Replace() create new strings. But we can also create new ones with a constructor.String Constructor
Literals. These specify string data. We use quotes around literal data (characters). Literals are embedded in the program executable.Literal
Comparisons. Are two strings equal? In a Dictionary, we can use a StringComparer to change how keys are compared. We can ignore case.StringComparison, StringComparer
Char. A string contains data made up of individual characters. We deal with these chars in looping constructs. Accessing chars is a fast way to test strings.CharChar: Literalchar.IsDigitchar.IsLowerchar.ToLower
TextInfo. This type provides some helpful methods. We can convert a string To Title Case with ToTitleCase. No custom methods are needed.TextInfoTextInfo: ToTitleCase
Whitespace. Strings often contain newline or whitespace. We often need to check for these values. We use methods like IsNullOrWhitespace.NewLineLine CountWhitespace
Empty. An empty string has zero characters. Meanwhile a null string is no string at all. It is a reference that points to no memory location.Empty StringNull Strings
Parse, TryParse. When we see a string like "1230" we usually want its numeric form. We use TryParse for this. Here we transform strings into other types. Many routines are built-in.int.Parse
Conversions. We can convert a list into a string. And we can convert a string into a byte array. These are some of the most common conversions.Convert: List, StringConvert: String, Byte ArrayConvert: String, Int
Custom methods. Many string algorithms are composed from the built-in methods. With Substring calls we can Truncate strings. With ToCharArray we can change characters.Custom: IsUpper, IsLowerCustom: Reverse StringCustom: RightCustom: Sort String LengthsCustom: ToCharArray, Edit CharsCustom: TruncateCustom: Uppercase First Letters
Custom methods, chars. Here are some custom char-based string algorithms. We test individual chars in strings. We use arrays to look up char values.Custom: ASCII TableCustom: Char Lookup TableCustom: Char TestCustom: Combine CharsCustom: Count CharactersCustom: Count Letter FrequenciesCustom: Duplicate CharactersCustom: Get Char at IndexCustom: Remove Chars
Explanations. I explain concepts of strings. How do we append strings when there is no append method? We also learn to increment strings.String AppendString IncrementString Property
StringBuilder. This is not a string, but it is used to build up or change strings. For appending strings in a loop, we likely want to use StringBuilder.StringBuilder
StringBuilder, methods. Here are some methods that the StringBuilder class provides. Some optimizations based on StringBuilder are shown.StringBuilder: Append, AppendLineStringBuilder: AppendFormatStringBuilder: CapacityStringBuilder: ClearStringBuilder: ToString
Optimization. In typical usage strings are fast. But they are sometimes used in an inefficient way. Often reducing string allocations is helpful.Optimization
A review. A string is immutable. It can be used in many methods, and none of them have to worry about data changes. It never becomes invalid. This reduces copies.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls