This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Simple String Manipulation Functions

The twenty-second part of the C# Fundamentals tutorial continues the examination of the string manipulation functionality provided by the String class. This article investigates some of the simple methods available to manipulate the text of a string.

String Concatenation

String concatenation is the act of combining two strings by adding the contents of one string to the end of another. Earlier in this tutorial we considered string operations including using the concatenation operator (+). We used the following example:

string start = "This is a ";
string end = "concatenated string!";

string concat = start + end;    // concat = "This is a concatenated string!"

The String class also provides a method for concatenating strings. The Concat method allows many strings to be passed as arguments. The strings are joined together and a new, concatenated string is returned:

string start = "This is a ";
string end = "concatenated string!";

string concat = string.Concat(start, end);

NB: As with previous string functions, the original strings are unchanged.

Inserting Strings into Strings

The String class provides a method that inserts one string into the middle of another. The Insert method acts upon an existing string variable or literal and requires two parameters. The first is an integer that indicates the position where the second string is to be inserted. This integer counts characters from the left with zero indicating that the insertion will be at the beginning of the string. The second parameter is the string to be inserted.

string template = "Please ask for  on arrival.";
string tutor = "Lisa";

Console.WriteLine(template.Insert(15, tutor));

// Outputs: "Please ask for Lisa on arrival."

Removing Characters from a String

The Remove method allows characters to be deleted from a string, shortening the string accordingly. There are two overloads available. The first requires a single parameter to indicate the start position for the character removal. The second overload adds a second parameter specifying how many characters to delete. Any further characters are unaffected.

string sample = "The quick brown fox jumps over the lazy dog.";

string result = sample.Remove(16);      // result = "The quick brown "
result = sample.Remove(16, 24);         // result = "The quick brown dog."

Extracting Text from a String

The String class provides a useful function to extract a piece of text from the middle of a string. This method has two overloads that are very similar to those of the Remove method. However, rather than removing the middle section of the string and keeping the start and end, the Substring method discards the start and end and returns the middle section. The following code illustrates this using the same parameters as the previous example.

string sample = "The quick brown fox jumps over the lazy dog.";

string result = sample.Substring(16);   // result = "fox jumps over the lazy dog."
result = sample.Substring(16, 24);      // result = "fox jumps over the lazy "

Search and Replace

All modern word processors and text editors include a search and replace function that permits a specified piece of text to be substituted with a second string. This functionality is provided by the Replace method. The method accepts two parameters. The first is the string to search for and the second is the string to use as a substitute. When executed, all instances of the first string are automatically replaced.

string sample = "The brown fox.";

string result = sample.Replace("brown", "red");   // result = "The red fox."

Copying Strings

The final simple string manipulation function to be considered in this article is the Copy method. This method creates a copy of an existing string. It provides the same functionality as assigning to a string directly.

string sample = "The brown fox.";

string result = string.Copy(sample);    // result = "The brown fox."
20 November 2006