Books of .Net/Interview Questions Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Saturday, September 17, 2016

Lesson 3 - String in C#

In this article, we will learn various types of String and how can we use them.
C# supports two forms of string literals: 
regular string literals 
verbatim string literals

A regular string literal consists of zero or more characters enclosed in double quotes, as in "hello", and may include both simple escape sequences (such as \t for the tab character) and hexadecimal and Unicode escape sequences.

You can see this tutorials online.


Let us see by DEMO
String name=”learning square”;
Console.WriteLine(name);
Console.ReadLine();
Output is learning square

If I want to print name in double quote or square in new line,we have to use escape sequence.
what is escape sequences?

escape sequences in C#
Character combinations consisting of a backslash (\) followed by a letter or by a combination of digits are called "escape sequences." 

Escape sequences are typically used to specify actions such as carriage returns and tab movements on terminals and printers. They are also used to provide literal representations of nonprinting characters and characters that usually have special meanings, such as the double quotation mark ("). The following table lists the ANSI escape sequences and what they represent.

Escape Sequences(from MSDN)
Escape Sequence
Represents
\a
Bell (alert)
\b
Backspace
\f
Formfeed
\n
New line
\r
Carriage return
\t
Horizontal tab
\v
Vertical tab
\'
Single quotation mark
\"
Double quotation mark
\\
Backslash
\?
Literal question mark
\ooo
ASCII character in octal notation
\xhh
ASCII character in hexadecimal notation
\xhhhh
Unicode character in hexadecimal notation if this escape sequence is used in a wide-character constant or a Unicode string literal.
For example, WCHAR f = L'\x4e00' or WCHAR b[] = L"The Chinese character for one is \x4e00".

Let us see by demo
Output // “learning square” double quote
String name=”\”learning square\””;
Console.WriteLine(name);
Console.ReadLine();
   
Output \\square in new line
        
String name=”learning \nsquare”;
Console.WriteLine(name);
Console.ReadLine();
                  

If we want to output as SERVER01\SQL, 
 
 string name = "SERVER01\\SQL"
output \\   C:\Program Files\Microsoft SQL Server
String name =” C:\\Program Files\\Microsoft SQL Server”; but it is not looking good.suppose,we have long path ,then we can write it as using verbatim
 string name = @"C:\Program Files\Microsoft SQL Server\"; 
 
 
A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character.

The C# compiler understand this type of string as verbatim.

Basically the @ symbol tells the string constructor to ignore escape characters and line breaks like \,\n,\t  backslash,new linw and tab


string name = "hello \nworld";               // hello     
                                                world
string name = @"hello \nworld";               // hello \nworld
 
In the case of name, the compiler will not process the \n when generating the output.

In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence.
In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines.

string e = "Joe said \"Hello\" to me";      // Joe said "Hello" to me

string f = @"Joe said ""Hello"" to me";   // Joe said "Hello" to me

No comments :

Post a Comment