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,
and textual data. The string type allows us to test and manipulate character data.

String face

Literals. These specify string data. We use quotes around literal data (characters). Strings are also dynamically created, as by concat.


Program:This program uses three string literals and concatenates them into a new string. Strings are immutable.

So:They are not changed in-place.
Instead they are copied into new objects as required.

Based on:

.NET 4.5

Program that uses string type: C#

using System;

class Program
    static void Main()
	string value = "dot";
	value += "net";
	string result = string.Concat(value, "perls");




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.

About part

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


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.

Insert: placing an element into a collection

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

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.

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.

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.

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.

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.

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

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

Padding: right and left

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.

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

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