9.7 The string type and string literals


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 \tname is\nMok"

  • "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\tWorld"; // 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\tWorld " “ with the ' \ ' and the ' t ' right in the middle of the two words.

 string temp2 =  @  "Hello\tWorld"; //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\t world";  <-- 2  11:     d = @"hello\t world";  <-- 3  12:     e = "\\server\share\file.txt";  <-- 4  13:     f = @"\server\share\file.txt";  <-- 4  14:     g = "one\ntwo\nthree";  <-- 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\t world 

(4)

 \server\share\file.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\t world \server\share\file.txt \server\share\file.txt one two three one two three A is for Apple \u0041 is for \x41pple 


From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net