As we have seen, the on-screen texts are written using WriteLine, delimited with brackets and double quotes. But... how can we write a double quote on the screen? We can use certain special characters, known as "escape sequences". They are written after a backslash (\). For example, \" displays double quotes, and \' a single quote, and \n advances to the next line on screen.
These special sequences are:
Sequence Meaning
\a Beeps (alert)
\b Backspace (deleted the last character)
\f Form feed (ejects a sheet into the printer)
\n New line (jumps to the next line)
\r Carriage Return (goes to beginning of line)
\t Horizontal tabulation
\v Vertical tabulation
\' Displays a single quote
\" Displays a double quote
\\ Displays a backslash
\0 Null character (NULL)
Let's see an example that uses the most common ones:
/*---------------------------*/
/* C# Example #30: */
/* example30.cs */
/* */
/* Escape sequences */
/* */
/* Intro to C#, */
/* Nacho Cabanes */
/*---------------------------*/
using System;
public class Example30
{
public static void Main()
{
Console.WriteLine("This is a sentence");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("and another, two lines apart");
Console.WriteLine("\n\nMore lines apart...\n\nand more");
Console.WriteLine("Double quotes: \", simple quotes \', slash \\");
}
}
Which would display:
This is a sentence
and another, two lines apart
More lines apart...
and more
Double quotes: ", simple quotes ', slash \
These escape sequences are sometimes laborious to manipulate. For example, when using a directory structure as: c:\\data\\examples\\course\\example1. In this case, we can use an "at" sign (@) before the text instead of the backslashes:
myPath = @"c:\data\examples\course\example1"
In this case, the problem arises when there are quotes in the middle of the string. To show these quotes, we just double them:
command = @"copy ""example document"" f:"
Suggested exercise:
· Create a program that asks the user to type five letters and display them on screen together, but in reverse order, and between double quotes. For example if the entered letters are o, l, l, e , h, it should display "hello".
No comments:
Post a Comment