C# Strings

Array Class Collections File Keyword String .NET ASP.NET Cast Compression Data Delegate Directive Enum Exception If Interface LINQ Loop Method Number Process Property Regex Sort StringBuilder Struct Switch Time Windows WPF

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

Literals are a way to specify string data. We use quote characters to enclose literal data. Strings are also dynamically created.

Literal
Split

Split separates parts of strings. We isolate and extract substrings with a single method call.
Many programs,
not just tutorials,
need to split
and join strings.

SplitJoin
About part

Search. These methods search string data for substrings. The most useful one is IndexOf. There are variants of IndexOf such as Contains.

IndexOfIndexOfAnyLastIndexOfLastIndexOfAnyContains
Add

Concat. When we concat strings, we put them together into a larger string. When we append, we put a string at the end of another one.

Concat
Remove

Insert, remove. We insert a string at any position into an existing one. We remove a series of character starting at any position.

InsertRemove
ABC: letters

Replace. String data may contain a series of characters we want to replace with another substring, in each place it is found. The Replace method is useful here.

Replace
Length property

Length. We never need to manually count the number of characters. Instead, we use the Length property, a simple memory read, on a string.

Length
Substring method example

Substring. We acquire a substring of any string with the Substring method. Also we can use Substring to truncate, or take the rightmost part, of a string.

SubstringTruncateRight
Not equal

Equals, compare. We compare two strings for equality. The string.Equals method is commonly used. But Compare is also helpful. With it, we develop sorting routines.

EqualsCompareStringComparerStringComparison
Exclamation mark

Starts, ends. It is often useful to test only the first few characters of a string for a certain value. StartsWith provides this ability. EndsWith does the opposite.

StartsWithEndsWith
New keyword, constructor invocation

Constructor. We typically do not need string constructors in programs. But they can be useful in specific situations. And with Copy, we duplicate string data.

String ConstructorCopyCopyTo
Format

Format. Strings are used to format data types. There are many other formatting patterns and adjustments you can make to these substitution markers.

string.FormatDateTime FormatToString
Int

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

TryParseDateTime.ParseEnum.Parseint.Parseint.TryParseHex Format
Upper: uppercase letters

Lower, upper. Sometimes a string may be in ALL CAPS. With simple methods, we can transform the casing of strings. The ToLower method changes all uppercase letters to lowercase ones.

ToUpperToLowerToLowerInvariantUppercase First LettersToTitleCaseTextInfoIsUpper, IsLower
Newline

Trim. This modifies space and newline characters in string data. Strings sometimes contain characters at their starts or ends that we do not want.

TrimTrimEndTrimStart
Padding: right and left

Pad strings. Trimming a string removes extra characters on either end. Padding a string instead adds extra characters. With padding, we create columns of text. We can justify text.

PadLeftPadRight
About part

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

Environment.NewLineIsNullOrWhitespaceWhitespaceLine Count
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 Stringsstring.EmptyIsNullOrEmpty
Char type example: letter C

Chars. A string contains data that is made up of individual characters. We deal with these chars in looping constructs. Accessing chars is often the fastest way to test strings.

CharChange CharactersString CharsString For-Loop
Method, a computer program unit

Methods. Strings have many methods. They interact with many language features. We use modifiers on strings. We use strings as parameters and properties.

InternIsInternedNormalize, IsNormalizedString ToStringString Property
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 Optimization

Explanations. I explain concepts of strings. How do we append strings when there is no append method? We also learn to increment strings.

String AppendIncrement String NumberExplode
StringBuilder

StringBuilder. This is not a string,
but it is used to build up
or change strings. For appending strings in a loop, you almost certainly want to use StringBuilder. It is much faster here.

StringBuilder
Copyright

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#