C# String manipulation, string constructors, string assignment, and the StringBuilder class
Why is string manipulation so important?Because, as programmers, we do so much of it. In fact, on some days, itseems that everything is string manipulation. If you know how toprogram with strings, you're a great way along the journey to becominga powerful programmer. Fortunately, C# provides facilities for workingwith and manipulating strings. In this tutorial we will explain thetypes and methods related to strings, so that you can make short workof most common string-manipulation tasks in your projects.
This tutorial provides the information you need about:
- Understanding C# Strings
- Working with the Char Type
- Control Characters
- String Assignment
- The String Constructor
- Verbatim Strings
- Using String Methods
- Understanding the StringBuilder Class
Understanding C# Strings
In C#, a text string is stored in a data type named string, which is analias to the System.String type. In other words, when you create astring, you instantiate a System.String object. In addition to itsinstance members, the System.String class has quite a few importantstatic methods. These methods don.t require a string instance to work.It.s important to understand that the string type is immutable. Thismeans that once it has been created, a string cannot be changed. No characters can be added or removed from it, nor can its length be changed. In those situations in which it appears that a string's contents have been changed, what has really happened is that a new string instance has been created.
But wait, you say, my strings change all the time when I do things like:
string str = "I am having a great deal of fun";
str += " on the DonationCoder website… ";
str += " The End";
Well, my friend, what this set of statements actually does is create a new instance of the variable str each time a value is assigned to str. In other words, in this example, an instance of strhas been created and a literal string value assigned to it three times.The statement that the type is immutable means that an instance cannotbe edited but it can be assigned.
Using an instance method without an assignmentis syntactically legal, but it doesn.t do anything (has no effect). Asa demonstration of my claim that an instance cannot be edited, examinethe following code:
using System;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
string str = "Mouser";
str.Replace("u", "U");
Console.WriteLine(str);
Console.ReadLine();
}
}
}
The above code does not change the contents of the variable str, because an instance cannot be edited. Run the above code in your IDE, and you'll see this result:

However, combining it with an assignment would work:
using System;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
string str = "Mouser";
str = str.Replace("u", "U");
Console.WriteLine(str);
Console.ReadLine();
}
}
}

The value of the newly assigned strinstance is, of course, MoUser. If you carefully compare the two sourcecode listings above, you'll see that the only difference occurs in thetwo highlighted lines. The second source code listing differs only byprefacing that line of code with str =, which tells the compiler to assign whatever follows the equal sign to the str instance of the String type. You may remember, from a previous tutorial in this series, that a type is defined by a class. There is, indeed, a String class. So, when I write string str = "Bryan";, what I've really done is created an instance of the String class called str, then assigned the text Bryan to it.
Is there any real problem with this use ofassignment in conjunction with strings? Well, no, it.s easy enough touse assignments when you need to change the value of a string variable.But instantiating and destroying strings every time you need to changeor edit one has negative performance consequences. If performance is anissue, you should consider working with instances of theSystem.Text.StringBuilder class, which are mutable (can be changed).The StringBuilder class is discussed later in this tutorial in a littlemore detail.
Once created, a string has a specific length.which, of course, is fixed (since the stringtype is immutable). Unlike in the C language, in which a string issimply an array of characters terminated with a zero byte, a C# stringis not a terminated array of characters. So, how does onedetermine the length of a C# string? As you probably know, you canquery the read-only Length property of the string instance tofind its length. Let's write a short console program demonstrating theuse of this property:
using System;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
string name = string.Empty;
do {
Console.Write("Please enter a name (enter "quit" to exit program): ");
name = Console.ReadLine();
if (name.Trim().ToLower() != "quit") {
Console.WriteLine(""{0}", is {1} characters in length.n", name, name.Length);
}
} while (name.Trim().ToLower() != "quit");
}
}
}
Now click on this link to see the program in action.
Working with the Char type
Char is a value type that contains a single Unicode character. Whilechars have a numeric value from hexadecimal 0×0000 through 0xFFFF, theyare not directly numerically usable without explicit type casting.
Literal values are assigned to char variablesusing singl
e quotes
. The literal can be a simple, single letter, or anescape sequence (for more on escape sequences, see the next section).Here are two examples:
char chr = 'P'; // contains capital P
char pi = 'u03A0.; // contains capital Pi
The char data type is related to the stringdata type: A string can be constructed from (or converted into) anarray of characters but string and char are two distinct types.
Suppose you have a string defined like this:
string str = "rotfl";
You can retrieve a character of the string using the string.s indexer. For example, the following statement
char chr = str[1];
stores the character .o. in the variable chr. But you cannot set a character in the string with a statement like str[1] = 'a';because the indexer property of the String class is read-only.(Actually, one would expect this in any case, because the stringinstance is immutable.)
If you use the Object Browser to have a look at the String class, you.ll see that its indexer property, declared as this[int], has a get accessor, but no set accessor.
Control Characters
The backslash() is a special control character, also called the escape character.The character after the backslash has a special significance. See thefollowing table for the meaning of each escape sequence:
|
Character<o:p></o:p> |
Meaning<o:p></o:p> |
|
|




