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
Strings. Nearly every program uses strings.
we find characters,
and textual data. The string type allows us to test and manipulate character data.
Literals are a way to specify string data. We use quote characters to enclose literal data. Strings are also dynamically created.Literal
Split separates parts of strings. We isolate and extract substrings with a single method call.
not just tutorials,
need to split
and join strings.
Search. These methods search string data for substrings. The most useful one is IndexOf. There are variants of IndexOf such as Contains.IndexOfIndexOfAnyLastIndexOfLastIndexOfAnyContains
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
Insert, remove. We insert a string at any position into an existing one. We remove a series of character starting at any position.InsertRemove
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. 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. 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
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
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
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. Strings are used to format data types. There are many other formatting patterns and adjustments we can make to these substitution markers.string.FormatDateTime FormatToString
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
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
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
Pad strings. Trimming a string removes extra characters on either end. Padding instead adds extra characters. With padding, we create columns of text. We can justify text.PadLeftPadRight
Newlines, whitespace. Strings often contain newline or whitespace. We often need to check for these values. We use methods like IsNullOrWhitespace.Environment.NewLineIsNullOrWhitespaceWhitespaceLine Count
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
Chars. A string contains data 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
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. 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 Number
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. It is much faster here.
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.