There are some special cases for creating literal strings.
For instance you might want quotes
as characters inside your string. In this case you need special
symbolism using a character escape code, starting with
Then the character after the backslash has a special meaning.
For instance a quote character after a backslash,
does not mean the end of a string literal. It means a quote character
is literally used in the string:
"He said, \"Hello!\", over and over."
We can illustrate with csharp, first with a simple string:
csharp> Console.WriteLine("Hello world!"); Hello world! csharp> Console.WriteLine("He said, \"Hello!\", over and over."); He said, "Hello!", over and over.
There are many other special cases of escape code. The main ones you are likely to use are:
Hence if you really want a backslash character in a literal, you need to write two of them.
The newline character indicates further text will appear on the next line down
when printed with the
csharp> Console.WriteLine("Windows path: c:\\Users\\aharrin"); Windows path: c:\Users\aharrin csharp> Console.WriteLine("a\nbc\n\ndef") a bc def
Literal strings that are simply delimited by quotes
must start and end on the same line.
There is also a notation for @-quoting, with an at-sign
@ before the first
quote. In an @-quoted string, all characters are treated verbatim, including
all backslashes. Also the string may go on for several lines, and all newlines
are included literally. (The csharp program does not recognize
multi-line @-quoted strings.)
This fragment in a program would produce the same output as the statements in
the csharp example above:
Console.WriteLine(@"Windows path: c:\Users\aharrin"); Console.WriteLine(@"a bc def");
The only thing this example does not show off well is the amount of left margin indentation. That is significant in a multiline @-quoted string. A whole simple program with this code is in example at_sign_strings/at_sign_strings.cs.
Caution: When you give csharp an expression evaluating to a string at the
prompt, you get back a verbatim string with quotes added around it,
@ to remind you that it is verbatim:
csharp> "Windows path: c:\\Users\\aharrin" "Windows path: c:\Users\aharrin" csharp> "a\nbc\n\ndef" "a bc def"
swith a single string literal that, when printed, shows something on one line then three empty lines, and then a final line with text.