C# String Manipulation

When working with a string of characters, C# has a number of built in methods that allow for them to be easily manipulated.

When working with the console, a string can be displayed using the ‘WriteLine’ method.

Console.WriteLine("Hello world!");

If it is necessary to incorporate the value of a variable into this message it can be done in a number of different ways.

string fname = "Bob";

Console.WriteLine("Hello " + fname + "!");
Console.WriteLine("Hello {0}!", fname);
Console.WriteLine($"Hello {fname}!");

All three of the above examples display the same message in the console, ‘Hello Bob!’. The first example uses the ‘+’ symbol to concatenate the variable into the string. The second example uses the zero in curly braces as a placeholder for the variable that has been specified after the comma. Note that if it is necessary to incorporate more than one variable into the string, the second placeholder would be a one in curly braces and the two variables to incorporate would be separated by a comma.

string fname = "Bob";
int age = 30;

Console.WriteLine("{0} is {1} years old.", fname, age);

The third method of incorporating variables into a string is known as string interpolation. It uses the dollar sign before the opening double quote and then the variables to be included are enclosed in curly braces.

Index Value

Every character in a string has an index value, which can be used to reference it. The first character has an index value of zero, the second has an index of one and so on.

string example = "This is a string.";

Console.WriteLine(example[3]);

Here, the index value of three in square brackets is used to output the fourth character of the string to the console.

It is also possible to find the index position of a character in a string using the ‘IndexOf’ method.

Console.WriteLine(example.IndexOf("i"));

Note that, the ‘IndexOf’ method will return the index position of the first occurrence of a character, if more than one exists in the string. There is also a ‘LastIndexOf’ method that can be used to return the index position of the last occurrence of a particular character.

Console.WriteLine(example.LastIndexOf("i"));

If the character does not exist in the string, then a ‘-1’ is returned.

Substring

Using the ‘Substring’ method, it is possible to return a portion of a string by specifying the index position to start from, together with the number of characters that are required.

string example = "This is a string.";

Console.WriteLine(example.Substring(5, 4));

The above example starts at index position five and displays four characters.

is a

Upper and Lower Case

In order to change a string to upper or lower case, there are two methods that can be used, ‘ToUpper’ and ‘ToLower’.

string example = "This is a string.";

Console.WriteLine(example.ToUpper());
Console.WriteLine(example.ToLower());

The first will change the whole string to upper case and the latter will convert it to lower case.

THIS IS A STRING.
this is a string.

Trim

If a string has extra spacing at the start, end, or both, it’s possible to remove this using the ‘Trim’ method as demonstrated below.

string example = "   This is a string.   ";

Console.WriteLine(example.Trim());

Length

Sometimes it is necessary to find the size or length of a string. This can be done using the ‘Length’ property.

string example = "This is a string.";

Console.WriteLine(example.Length);

Insert, Remove and Replace

In order to manipulate the contents of a string the ‘Insert’, ‘Remove’ and ‘Replace’ methods can be used.

The ‘Insert’ method can be used to insert a string, into an existing string at the specified index position.

string example = "This is a string.";

Console.WriteLine(example.Insert(9, " short"));

Here a space and the word ‘short’ is inserted into the string at index position nine, just after the ‘a’, to produce the string, ‘This is a short string.’.

The ‘Remove’ method can be used to delete a part of a string, by specifying the index position to start the deletion from, as well as the number of characters to delete. Note that, if the number of characters to delete is omitted then all characters from the index position to start from, to the end of the string, will be removed.

string example = "This is a short string.";

Console.WriteLine(example.Remove(9, 6));

In the above example, the deletion starts at index position nine, just after the ‘a’, and removes six characters, to produce the string, ‘This is a string.’.

Finally, the ‘Replace’ method can be used to replace a specified part of a string with some other text.

string example = "This is a short string.";

Console.WriteLine(example.Replace("short", "small"));

Here, the word ‘short’ is replaced with the word ‘small’ to produce the string, ‘This is a small string.’.

The properties and methods discussed above are by no means exhaustive. There are many others available.