7. To print newline
( \n ) is used to shift pointer to next line during run-time.
Practical Implementation:
using System;
namespace AspnetSolutions
{
class stringEscapeSequences
{
public static void Main()
{
namespace AspnetSolutions
{
class stringEscapeSequences
{
public static void Main()
{
/*To print newline*/
Console.WriteLine("newline = Imagination\nhunt");
Console.WriteLine("newline = Imagination\nhunt");
As
you can see that In the above example we want "imagination" and "hunt"
to be written in two separate lines. And using (\n) we move the "hunt"
printed in the next line.
8. To print carriage return
( \r ) is a carriage return character, it tells your interpreter to move the cursor at the start of the line i.e., to the left side. The cursor is now at the position where the next characters will be rendered. So, printing a ( \r ) allows to override the current line interpreted.
Practical Implementation:
using System;
namespace AspnetSolutions
{
class stringEscapeSequences
{
public static void Main()
{
/*To print carriage return.*/
Console.WriteLine("Carriage return = Imagination\rhunt");
namespace AspnetSolutions
{
class stringEscapeSequences
{
public static void Main()
{
/*To print carriage return.*/
Console.WriteLine("Carriage return = Imagination\rhunt");
}
}
}
}
}
The program is printing "Carriage return = Imagination"
,
then it is moving the cursor back to the beginning of the line. And then it will print "hunt". It appears the
beginning of the string is being overwritten as "huntiage return = Imagination".
9. To print horizontal tab
Horizontal
Tab escape sequence ( \t ) is similar like pressing tab button from
keyboard. It will make the cursor to give 3 or 4 spaces.
Practical Implementation:
using System;
namespace AspnetSolutions
{
class stringEscapeSequences
{
public static void Main()
{
namespace AspnetSolutions
{
class stringEscapeSequences
{
public static void Main()
{
/*To print horizontal tab*/
Console.WriteLine("horizontal tab = Imagination\thunt\tblog\tspot");
Console.WriteLine("horizontal tab = Imagination\thunt\tblog\tspot");
}
}
}
}
}
To read STRING (ESCAPE SEQUENCES) IN C# Part 1 Blog - Click here
RELATED QUESTIONS
Q-1 Which of the following options are correct for an escape sequence?
A) r
B) \
C) b
D) $
Ans- Option (A, B and C).
Q-2 Which of the following are not correct escape sequences?
A) r, p, b, '
B) \, ", f, n
C) b, $, t, r
D) p, ?, \, n
Ans- Option (A, C, and D).
Explanation: Because p and $ are not an escape sequence.
Click imaginationhunt.blogspot to see latest Blogs
Keep learning and sharing...
No comments:
Post a Comment