STRING (ESCAPE SEQUENCES) IN C#

Today in Engineer's World, we are discussing a very important topic of C# -String in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS

Click imaginationhunt.blogspot to see latest Blogs
 

What are Escape Sequences?

An escape sequence is a series of characters that begins with a backslash ( \ ), followed by combination of one or more letters or digits. To represent a newline character, single quotation mark, or certain other characters in a character constant, you must use Escape Sequences. 
Note: 
a. Escape sequence work only during run-time.
b. They always have to enclose within double quotes.

What are the various escape sequences that are available in C#?
The various escape sequences that are available in C# are as follows: 

1. To provide Single Quote
This escape sequence ( \' ) is used to put your string value or variable value into single quote.  

Practical Implementation:
 
using System;

namespace AspnetSolutions
{
    class stringEscapeSequences
    {
        public static void Main()
        {
            /*To provide Single Quote*/
            Console.WriteLine("\'Imaginationhunt\'");

            /*Or single quote to variables*/
            string str1 = "Imaginationhunt";
            Console.WriteLine("\'" + str1 + "\'");
         }
    }
}




2. To provide Double Quote
This escape sequence ( \" ) is used to put your string value or variable value into double quote.

Practical Implementation:

using System;

namespace AspnetSolutions
{
    class stringEscapeSequences
    {
        public static void Main()
        {
            /*Double Quote to hard coded string*/
            Console.WriteLine("\"Imaginationhunt\"");

            /*Or double quote to variables*/
            string str2 = "Imaginationhunt";
            Console.WriteLine("\"" + str2 + "\"");
        }
    }
}




3. To provide Backslash
This escape sequence ( \\ ) is mainly used in path. We input (\\) and at run-time it change to slash (\) between directories so as to separate them.
 
Practical Implementation:

using System;

namespace AspnetSolutions
{
    class stringEscapeSequences
    {
        public static void Main()
        {
            /*To provide Backslash*/
            Console.WriteLine("C:\\Abc\\Def");
        }
    }
}



If we directly give the path as C:\Abc\Def, we get a compile time error which says that unrecognized escape sequence.

To read STRING (ESCAPE SEQUENCES) IN C# Part 2 Blog - Click here

RELATED QUESTIONS:-

#newtoprgm try firstprogram

Q-1 What the error in code?
Code:     Console.WriteLine("Hello Imaginationhunt" + "." \b + "blogspot");
Ans- Unexpected character '\'.

Q-2 Which escape sequence is used to delete a character?
A) \b 
B) \r
C) \\
Ans- Option (A).

Click imaginationhunt.blogspot to see latest Blogs

Keep learning and sharing...

Comments

Popular posts from this blog

SSC CGL 2016 Tier-1 Marks / Result

DATA TYPES IN C#

Pattern printing program

IS STRING MUTABLE OR IMMUTABLE IN .NET?

Top Mutual Funds

SSC CGL 2017 TIER 2 RESULT OUT

JUST IN TIME (JIT)

Can we have multiple Main methods in one class

TOP 15 VISUAL STUDIO SHORTCUT KEYS

Top 10 Interview Tips and Questions