Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft CSharp Programming For The Absolute Beginner (2002) [eng]-1.pdf
Скачиваний:
46
Добавлен:
16.08.2013
Размер:
15.71 Mб
Скачать

Combining String Values

The ability to write string values to the screen is very useful, but you should know about a couple other special circumstances. Sometimes you want to combine string variables in other ways. Also, you often want to type special characters, such as the tab character or quotation marks, to the screen or force a carriage return at a specific place. Take a look at the program in Figure 1.15, which illustrates some interesting printing problems.

Figure 1.15: This program demonstrates several interesting problems.

Take a look at the source code for this program. You will see that it demonstrates techniques that can be very useful as you write characters to the screen:

using System;

namespace concat

{

///<summary>

///Demonstrates string concatenation,

///escaped characters

///</summary>

class concat

{

static void Main(string[] args)

{

string userName = "Jacob"; Console.WriteLine("This is regular text"); Console.WriteLine("Hi there, " + userName + "!"); Console.WriteLine("This line has a \t tab in it");

Console.WriteLine("This line has a \n newline in it"); Console.WriteLine("This line has a \\ slash in it"); Console.WriteLine("This line has \"quotes\" in it"); Console.WriteLine();

Console.WriteLine("Press Enter to continue"); Console.ReadLine();

} // end main } // end class

}// end namespace

The program consists of several lines written to the screen. The first line is typical, but each of the others illustrates a different technique for printing to the screen.

26

Combining Strings with Concatenation

The program has a string variable named userName. You can see that I have printed out the value of the variable, but I used a technique different from the one described earlier in the chapter. You can use a plus (+) sign to combine literal string variables (whatever is contained inside quotes) and string variables.

Computer scientists like to create complicated names for simple concepts, and this is no exception. Using plus signs like this to combine string objects is called string concatenation.

Trick Having two ways to do the same thing might seem strange, but it makes a lot of sense. For ordinary situations, you often use the interpolation trick shown at the beginning of this chapter, but in certain situations, concatenation makes more sense.

Adding a Tab Character

Sometimes you will want to send information to the screen that would be easy with a keyboard, but not so simple when you are writing a program. For example, the next line of code looks like this:

Console.WriteLine("This line has a \t tab in it");

You can see the \t combination, which is a backslash followed by a t character. This special combination stands for tab. Whenever the compiler encounters this sequence, it acts as if the Tab key was pressed. If you look up at the output of this program, you see a gap where the \t combination was placed in the original code.

Using the Newline Sequence

C# allows you to use some other special characters. Perhaps the most useful is the newline character, which is the combination \n. Whenever the compiler sees this sequence, it adds a carriage return. If you look again at the output, you see that the line breaks exactly where the \n sequence occurs in the original string.

Hint Console−based applications (such as the ones in this chapter) do not have any sort of word wrap. If you are writing a long complicated string to the screen, you might have to insert newline sequences to ensure that the lines are separated appropriately.

Displaying a Backslash

Because the backslash is used in all these special sequences, you might wonder how you display the backslash character. All that’s necessary is to include two backslashes together, as I did in this line:

Console.WriteLine("This line has a \\ slash in it");

Displaying Quotation Marks

Sometimes you want to show quotation marks in text. This can be difficult because the quote symbol is also used to determine where the string begins and ends.

27

Соседние файлы в предмете Программирование на C++