Using special characters in strings

C# enables you to use a special syntax to embed special characters in your string. These special characters are listed in Table 3-6.

Table 3-6: C# Special Characters

Characters

Purpose

\t

The special characters \t embed a tab into the string. A string defined as hello\tthere is stored in memory with a tab character between the words hello and there

\r

The special characters \r embed a carriage return into the string. A string defined as hello\rthere is stored in memory with a carriage return character between the words hello and there. The carriage return character returns the cursor to the beginning of the line but does not move the cursor down a line.

\v

The special characters \v insert a vertical tab into the string. A string defined as hello\vthere is stored in memory with a vertical tab character between the words hello and there.

\f

The special characters \f insert a form-feed character into the string. A string defined as hellofthere is stored in memory with a form-feed character between the words hello and there. Printers usually interpret a form-feed character as a signal to advance to a new page.

\n

The special characters \n insert a newline into the string. A string defined as hello\nthere is stored in memory with a newline character between the words hello and there. The software development community has long debated the interpretation of the newline character. It has always meant, "move the next output position down one line." The question is whether the

Table 3-6: C# Special Characters

Characters

Purpose

operation also includes moving the next position to the first character on the previous line. The .NET Framework interprets the newline character as both moving down a line and returning the next character position to the beginning of the next line. If you are unsure, you can always write the special characters \n and \r together.

\x

The special characters \x enable you to specify an ASCII character using two hexadecimal digits. The two hexadecimal digits must immediately follow the \x characters and must be the hexadecimal value of the ASCII character that you want to output. For example, the ASCII space character has an ASCII character code of decimal 32. The decimal value 32 is equivalent to the hexadecimal value 20. Therefore, a string defined as hello\x20there is stored in memory with a space character between the words hello and there.

\u

The special characters \u enable you to specify a Unicode character using exactly four hexadecimal digits. The four hexadecimal digits must immediately follow the \u characters and must be the hexadecimal value of the Unicode character that you want to output. For example, the Unicode space character has a Unicode character code of decimal 32. The decimal value 32 is equivalent to the hexadecimal value 20. Therefore, a string defined as hello\u0020there is stored in memory with a space character between the words hello and there. Be sure to use exactly four digits after the \u characters. If the value is less than four digits, use leading zeros to pad your value to four digits.

\\

The special characters \\ enable you to specify a backslash character at the current position. A string defined as hello\\there is stored in memory with a backslash character between the words hello and there. The reasoning behind having two backslashes is simple: Using a single backslash might cause the C# compiler to mistake it as the start of another special character. For example, suppose that you forget the second backslash and write hello\there in your code. The C# compiler sees the backslash and the t in the word there and mistakes it for a tab character. This string would then be stored in memory with a tab character between the words hello and here. (Remember that the t in there would be interpreted as the tab character and would not be a part of the actual word.)

0 0

Post a comment