In the topics that follow, you’ll learn some basic skills for working with the string data type. These skills should be all you need for many of your applications. Then, in chapter 9, you’ll learn the skills you need for advanced string operations.
How to declare and initialize a string
A string can consist of any letters, numbers, and characters. Figure 4-8 summarizes the techniques that you can use to work with string variables. To start, you use the string keyword to declare a string. Then, you can assign a string literal to a string by enclosing the characters within double quotes.
To assign an empty string to a variable, you can code a set of double quotes with nothing between them. You do that when you want the string to have a value, but you don’t want it to contain any characters. A third alternative is to assign a null value to a string by using the null keyword, which usually indicates that the value of the string is unknown.
How to join and append strings
Figure 4-9 also shows how to join, or concatenate, two or more strings into one string. To do that, you use the + operator as shown in the second example in this figure. Here, two string variables are concatenated with a string literal that consists of one space. The result is then stored in another string variable.
You can also join a string with a value data type. This is illustrated in the third example in this figure. Here, a variable that’s defined with the double data type is appended to a string. When you use this technique, C# automatically converts the value to a string.
You can also use the + and += operators to append a string to the value of a string variable. This is illustrated in the last two examples in this figure. Notice that when you use the + operator, you include the string variable in the expression that you’re assigning to this variable. In contrast, when you use the += operator, you can omit the string variable from the expression. Because of that, it’s common to use this operator to simplify your code.
How to declare and initialize a string
string message1 = "Invalid data entry.";
string message2 = "";
string message3 = null;
How to join strings
string firstName = "Bob"; // firstName is "Bob"
string lastName = "Smith"; // lastName is "Smith"
string name = firstName + " " + lastName; // name is "Bob Smith"
How to join a string and a number
double price = 14.95;
string priceString = "Price: $" + price; // priceString is "Price: $14.95"
How to append one string to another string
string firstName = "Bob"; // firstName is "Bob"
string lastName = "Smith"; // lastName is "Smith"
string name = firstName + " "; // name is "Bob "
name = name + lastName; // name is "Bob Smith"
How to append one string to another with the += operator
string firstName = "Bob"; // firstName is "Bob"
string lastName = "Smith"; // lastName is "Smith"
string name = firstName + " "; // name is "Bob "
name += lastName; // name is "Bob Smith"
How to include special characters in strings
Figure 4-9 shows two techniques that you can use to include certain types of special characters within a string. In particular, this figure shows how to include backslashes, quotation marks, and control characters such as new lines, tabs, and returns.
One technique you can use to include these characters in a string is to use the escape sequences shown in this figure. Although these escape sequences are the ones you’ll use most often, C# provides other escape sequences for hexadecimal and Unicode characters.
If you’re assigning a string literal to a string, you may prefer to use a verbatim string literal instead of escape sequences. To use a verbatim string literal, you code an @ sign before the opening quote for the string. Then, you can enter backslashes, tabs, and new line characters between the opening and closing quotes. For example, you can use the Enter key to enter one or more new line characters. Then, the verbatim string literal will span two or more lines.
Although verbatim string literals work well for literals that include backslashes and single quotes, a complication occurs when you need to include a double quote in the literal. That’s because double quotes are used to indicate the beginning and end of the literal. To include a double quote in a verbatim string literal, then, you must enter two double quotes.
At this point, you may be wondering when you should use escape sequences to include special characters in a string and when you should use verbatim string literals. The answer is that each technique is appropriate for certain types of coding situations. For example, verbatim string literals work well for coding file locations. On the other hand, it’s often easier to use escape sequences to include new line characters and tabs in a string. Because of that, you’ll want to become familiar with both techniques. Then, you can decide which technique works best for a given situation.