Most software needs a way to store human-readable data in the form of characters and words. This chapter explores the use of string and character data types. The topic of building strings from multiple variables using a concept known as string interpolation is also covered.
Character data type
When talking about characters, we are referring to individual letters and numbers. For example, the letter ‘a’ is a character, as is the visual representation of the number ‘1’. Such characters may be stored in a C# char
type. A char
can contain one character and one character only. It is important to be aware that a character is not limited to those in the English alphabet. Characters are stored internally in C# as grapheme clusters. A grapheme cluster combines two or more Unicode scalars to represent a single visible character.
In addition, characters can be used for counting in loops and even in mathematical expressions. To assign a character to a variable or constant, surround the character with single quotes:
char myVar = 'a'; const char myConst = 'b';
The following lines assign a variety of different characters to char
type variables:
char myChar1 = 'f'; char myChar2 = ':'; char myChar3 = 'X';
Characters may also be referenced using Unicode code points. The following example assigns a smiley face symbol (☺) to a variable using Unicode and then prints it:
char myChar4 = '\u263A'; System.Console.WriteLine(myChar4);
Output:
☺
Special characters
In addition to the standard set of characters outlined above, a range of special characters (also called escape sequences) is available for specifying items such as a new line, tab, or a specific Unicode value within a string. These special characters are identified by prefixing the character with a backslash (a concept referred to as escaping).
The escape sequence for a new line special character, for example, is \n
. The following code fragment uses this new line special character to add blank lines within a string:
System.Console.WriteLine("This is a line of text.\n\n\nAn this is another.");
Output:
This is a line of text.
An this is another.
Any character preceded by a backslash is considered a special character and treated accordingly. This raises the question as to what to do if you actually want a backslash character. This is achieved by escaping the backslash itself (\\
):
System.Console.WriteLine("C# special characters are escaped using the \\ character.);
Output:
C# special characters are escaped using the \ character.
Commonly used special characters supported by C# are as follows:
\n
– Newline\r
– Carriage return\t
– Horizontal tab\\
– Backslash\”
– Double quote (used when placing a double quote into a string declaration)\’
– Single quote (used when placing a single quote into a string declaration)\unn
– Single-byte Unicode scalar where nn is replaced by two hexadecimal digits representing the Unicode character.\unnnn
– Double-byte Unicode scalar where nnnn is replaced by four hexadecimal digits representing the Unicode character.\unnnnnnnn
– Four-byte Unicode scalar where nnnnnnnn is replaced by eight hexadecimal digits representing the Unicode character.
String data type
Previously, we looked at storing individual characters in a char variable. While this works for storing a single letter, number, or Unicode scalar, it is of little use for storing entire words or sentences. For this purpose, the string
variable type is supported by C#. Constants or variables of type string
can store a string of any number of characters.
String literal values are surrounded by double quotes ("
). For example:
string myString = "This is string is literal.";
Verbatim string literals
A regular string
value will interpret special characters. The following code, for example, consists of a string containing multiple special characters:
string myString = "This is some text\n\n\tand this is some more.";
As shown above, the only way to make the text span multiple lines is to use the \n
escape character when using a standard string literal. The following code, for example, will not compile:
string myString = "This is some text and this is some more.";
For a string literal to be interpreted precisely as written, it must be declared as a verbatim string literal type.
Verbatim string literals ignore the special meaning of escape characters and can span multiple lines. To declare a string literal as verbatim, prefix the declaration with the @
character. For example, this allows literals such as the following to be expressed:
string myString = @"Hi Team, The file located at 'C:\Users\demo\newdata.txt' uses the \t sequence to insert tabs into text. Best regards John"; System.Console.WriteLine(myString);
Output:
Hi Team,
The file located at 'C:\Users\demo\newdata.txt'
uses the \t sequence to insert tabs into text.
Best regards
John
String interpolation
Strings may also be constructed using combinations of strings, variables, constants, expressions, and function calls using string interpolation.
This involves embedding items into a string by prefixing the literal declaration with $
character and wrapping the item as follows where {item} is a constant or variable name, a function call, or even an expression such as a mathematical calculation:
int count = 10; string = $"The current count is {count}";
The following code, for example, creates a new string
variable using string interpolation to embed content from a variety of sources before outputting it to the console:
string userName = "John"; int inboxCount = 25; int maxCount = 100; var message = $"{userName} has {inboxCount} messages. Message capacity remaining is {maxCount - inboxCount}."; System.Console.WriteLine(message);
Output:
John has 25 messages. Message capacity remaining is 75.
If you need to wrap content in braces ({}
) without it being interpreted as interpolated values, use double braces ({{}}
) as follows:
string userName = "John"; var message = $"{userName} reported seeing error code 10 {{low battery}}."; System.Console.WriteLine(message);
Output:
John reported seeing error code 10 {low battery}.
Interpolation may also be used within verbatim string literals by prefixing the string with either [email protected]
or @$
:
string userName = "John"; int inboxCount = 25; int maxCount = 100; var message = [email protected]"{userName} has {inboxCount} messages. Message capacity remaining is {maxCount - inboxCount}."; System.Console.WriteLine(message);
Output:
John has 25 messages.
Message capacity remaining is 75.
Note: Interpolated string literals can only be assigned to variables. An attempt to assign an interpolated string to a constant will result in a compiler error.