BlackWaspTM
C# Programming
.NET 1.1+

C# String Data Type (2)

The sixteenth part of the C# Fundamentals tutorial begins an examination of the string data type. This is possibly the most important native data type in the C# programming language. Using strings, we can perform processing of textual information.

String Constructors

All classes contain one or more constructors. A constructor is a special method that is executed when a new object of that class is instantiated. Constructors prepare the object's values and obtain any resources that it may require, sometimes using provided arguments you provide.

The string class defines several constructors. Most are beyond the scope of this tutorial. One interesting constructor allows you to specify a character and an integer to create a string containing a single character repeated many times. This constructor is used in the following example. Note the use of the new keyword that is used whenever objects are instantiated in this way, and the constructor parameters within parentheses.

string repeating = new string('.', 15);   // repeating = "..............."

Escape Characters

There are some characters that cannot be easily included in a string literal. For example, a quotation mark cannot be included directly as it signifies the end of a literal. The following code fails to compile.

string invalid = "This is a "string".";
Console.WriteLine(invalid);

To include a quotation mark in a literal, a special code or escape character sequence is inserted. Escape character sequences are supplied as a backward slash (\) followed by one or more characters that determine the information to be added. For a quotation mark, the escape code is a backward slash and a quotation mark (\"). This is shown below:

string valid = "This is a \"string\".";
Console.WriteLine(valid);                // Outputs: This is a "string"

A similar problem exists for character literals where the apostrophe character is required. As you may expect, you can use a backslash and an apostrophe (\').

char apostrophe = '\'';

There are several other escape characters. They include the following:

Escape CharacterPurpose
\\Backslash (\). As the backward slash character defines as escape character sequence, the double-backslash is required to indicate the insertion of a backslash.
\nNew Line. When outputted, the output starts a new line of text when the control character is reached.
\tHorizontal Tab. Inserts a horizontal tab into a text string, moving the current position to the next tab stop in a similar manner to pressing the tab key in a word processor.
\0Unicode Character Zero / Null. This character is used as a marker at the end of a file or data stream.
\aAlert. In some scenarios this character sounds an alert through the computer's speaker.
\bBackspace. Emits a backspace character.
\fForm Feed. Instructs a printer to eject the current sheet of paper and ready the next sheet.
\rCarriage Return. This is similar to the new line character when used for screen output. Some printers use this as an indicator to return to the start of the current line. In this case, to begin a new line both a carriage return and new line character (\r\n) are required.
\vVertical Tab. Inserts a vertical tab.
\uxxxxInserts the character with the Unicode character number xxxx. The xxxx portion is a four digit hexadecimal number.

Verbatim String Literals

C# supports a second type of string literal known as a verbatim string literal. This does not require the use of escape characters to define special characters. Instead, any information in the source code, including new lines, is included in the string. To define a verbatim literal prefix your string with an @ symbol before the opening quotation mark. The only character that requires a different action is the quotation mark itself, which must be entered twice to indicate a single character.

string literal = @"Literal strings can include backslash (\) characters.";

string quoted = @"This is a ""string"".";

string multiline = @"This is line one.
This is line two.";
22 October 2006