9.7. The string type and string literals

There are two types of string literals in C#:

  • regular string literals;

  • verbatim string literals.

What we have seen and used so far are regular string literals. These consist of characters enclosed in double quotation marks and may contain escape sequences, hexadecimal (x) and Unicode (u) escape sequences within. Examples of regular string literals include the following:

  • "Hello!"

  • "My name is Mok"

  • "Apple starts with the letter u0041, Banana starts with the letter x42"

Verbatim string literals are string literals which are 'what they seem'. If you include a @ sign in front of a string literal, everything between the double quotes will be considered part of the string 'as they are'. The @ symbol, in this case, is acting as a verbatim string prefix.

For example, the string temp1 in the statement below will represent a "Hello", followed by a tab, and a "World":

string temp1 = "Hello	World"; // regular string literal

If you want to include the two characters '' and 't' in between the two words, you have to use the backslash escape sequence and do something like this:

string temp1 = "Hello\tWorld"; // regular string literal

You can also use verbatim strings to do the same thing. The string temp2 in the statement below will represent "Hello World" – with the '' and the 't' right in the middle of the two words.

string temp2 = @"Hello	World"; //verbatim string literal

String literals are useful if you don't want something to be treated as escape sequences in your string. The simple program below gives more examples showing how to use string literals.

 1: using System;
 2:
 3: public class TestClass{
 4:   public static void Main(){
 5:
 6:     string a,b,c,d,e,f,g,h,i,j;
 7:
 8:     a = "hello, world";        <-- 1
 9:     b = @"hello, world";       <-- 1
10:     c = "hello	 world";        <-- 2
11:     d = @"hello	 world";        <-- 3
12:     e = "\\server\share\file.txt";        <-- 4
13:     f = @"\serversharefile.txt";       <-- 4
14:     g = "one
two
three";        <-- 5
15:     h = @"one       <-- 5
16:     two
17:     three";
18:     i = "u0041 is for x41pple";        <-- 6
19:     j = @"u0041 is for x41pple";        <-- 7
20:
21:
22:     Console.WriteLine(a);
23:     Console.WriteLine(b);
24:     Console.WriteLine(c);
25:     Console.WriteLine(d);
26:     Console.WriteLine(e);
27:     Console.WriteLine(f);
28:     Console.WriteLine(g);
29:     Console.WriteLine(h);
30:     Console.WriteLine(i);
31:     Console.WriteLine(j);
32:   }
33: }

(1)

hello, world

(2)

hello,    world

(3)

hello	 world

(4)

\serversharefile.txt

(5)

one
two
three

(6)

A is for Apple

(7)

u0041 is for x41pple

Output:

c:expt>test
hello, world
hello, world
hello    world
hello	 world
\serversharefile.txt
\serversharefile.txt
one
two
three
one
two
three
A is for Apple
u0041 is for x41pple

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.211.106