The string object is created using a literal string a

Info icon This preview shows pages 342–345. Sign up to view the full content.

View Full Document Right Arrow Icon
The string object is created using a literal string —a sequence of characters enclosed in double quotes: "I've gone all vertical." We’re already quite familiar with initializing a string with a literal—we probably do it without a second thought; but let’s have a look at these literals in a little more detail. Literal Strings and Chars The simplest literal string is a set of characters enclosed in double quotes, shown in the first line of Example 10-2 . Example 10-2. A string literal string myString = "Literal string"; Console.WriteLine(myString); This produces the output: Literal string 318 | Chapter 10: Strings
Image of page 342

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
You can also initialize a string from a char[] , using the appropriate constructor. One way to obtain a char array is by using char literals. A char literal is a single character, wrapped in single quotes. Example 10-3 constructs a string this way. Example 10-3. Initializing a string from char literals string myString = new string(new [] { 'H', 'e', 'l', 'l', 'o', ' ', '"', 'w', 'o', 'r', 'l', 'd', '"' }); Console.WriteLine(myString); If you compile and run this, you’ll see the following output: Hello "world" Notice that we’ve got double-quote marks in our output. That was easy to achieve with this char[] , because the delimiter for an individual character is the single quote; but how could we include double quotes in the string, without resorting to a literal char array? Equally, how could we specify the single-quote character as a literal char ? Escaping Special Characters The way to deal with troublesome characters in string and char literals is to escape them with the backslash character. That means that you precede the quote with a \ , and it interprets the quote as part of the string, rather than the end of it. Like this: "Literal \" string \" " If you build and run with this change, you’ll see the output, with quotes in place: Literal "string" There are several other special characters that you can escape in this way. You can find some common ones listed in Table 10-1 . Table 10-1. Common escaped characters for string literals Escaped character Purpose \" Include a double quote in a string literal. \' Include a single quote in a char literal. \\ Insert a backslash. \n New line. \r Carriage return. \t Tab. There are also some rather uncommon ones, listed in Table 10-2 . In general, you don’t need to worry about them, but they are quite interesting. † We’ll just show the string literal from here on, rather than repeating the boilerplate code each time. Just replace the string initializer with the example. Literal Strings and Chars | 319
Image of page 343
Table 10-2. Less common escape characters for string literals Escaped character Purpose \0 The character represented by the char with value zero (not the character '0' ). \a Alert or “Bell”. Back in the dim and distant past, terminals didn’t really have sound, so you couldn’t play a great big .wav file beautifully designed by Robert Fripp every time you wanted to alert the user to the fact that he had done something a bit wrong. Instead, you sent this character to the console, and it beeped at you, or even dinged a real bell (like the line-end on a manual typewriter). It still works today, and on
Image of page 344

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 345
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern