C# Strings

Array Collections File Keyword String .NET Cast Class Data Dictionary Enum Exception If Interface Lambda LINQ List Loop Method Number Process Property Regex Sort Split StringBuilder Struct Substring Switch Time Windows

String face

Strings. Nearly every program uses strings.
In strings,
we find characters,
words
and textual data. The string type allows us to test and manipulate character data.


String face

Methods. Here are the methods and properties on strings. Some are static. We access them on the string type (like string.Empty). Complex algorithms often use many string methods.

CompareCompareOrdinalCompareToConcatContainsCopyCopyToEndsWithEmptyEqualsFormatGetHashCodeIndexOfIndexOfAnyInsertInternIsInternedIsNormalizedIsNullOrEmptyIsNullOrWhiteSpaceJoinLastIndexOfLastIndexOfAnyLengthNormalizePadLeftPadRightRemoveReplaceSplitStartsWithSubstringToCharArrayToLowerToLowerInvariantToStringToUpperToUpperInvariantTrimTrimEndTrimStart
Constructor: create new

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
Convert

Comparisons. Are two strings equal? In a Dictionary, we can use a StringComparer to change how keys are compared. We can ignore case.

StringComparerStringComparison
Int

Parse, TryParse. These transform strings into other types. Many parsing routines are built-in. Usually it is a bad idea to create our own if one already exists.

TryParseDateTime.ParseEnum.Parseint.Parseint.TryParseHex Format
Squares: grey

TextInfo. This type provides some helpful methods. We can convert a string To Title Case with ToTitleCase. No custom methods are needed.

TextInfoTextInfo: ToTitleCase
Newline

Whitespace. Strings often contain newline or whitespace. We often need to check for these values. We use methods like IsNullOrWhitespace.

NewLineLine CountWhitespace
None

Empty. Does life have meaning? Or is it just emptiness? I have no idea. It is easier (and more fun) to test strings for emptiness.

Empty StringsNull Strings
Char type example: letter C

Chars. 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.

CharString Chars
Loop

Loops. We can loop over a string with any style of loop. When no index is required, a foreach-loop will result in the clearest code.

ForeachFor, string
Performance

Performance. In typical usage strings are fast. But they are sometimes used in an inefficient way. Often reducing string allocations is helpful.

Memory UsageEquals PerformanceReplace LogicReplace CharsToString Cacheint.Parse OptimizationToString FormatsToLower OptimizationIsUpper, IsLower
Method

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.

TruncateRightUppercase First LettersChange Characters
About part

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

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
Summary: 300, 200 and 100

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 and makes programs more robust.

C#