Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

วิธีพิมพ์ Escape Characters ใน C#

ตาราง Escape Characters ใน C# และวิธีแสดงผล

Escape CharคำอธิบายPatternDisplay
\aBell (\u0007)\a"\u0007" ใน "Warning!" + '\u0007'
\bBackspace (\u0008)[\b]{3,}"\b\b\b\b" ใน "\b\b\b\b"
\tTab (\u0009)(\w+)\t"Name\t", "Addr\t" ใน "Name\tAddr\t"
\rCarriage Return (\u000D)\r\n(\w+)"\r\nHello" ใน "\r\Hello\nWorld."
\vVertical Tab (\u000B)[\v]{2,}"\v\v\v" ใน "\v\v\v"
\fForm Feed (\u000C)[\f]{2,}"\f\f\f" ใน "\f\f\f"
\nNew Line (\u000A)\r\n(\w+)"\r\nHello" ใน "\r\Hello\nWorld."
\eEscape (\u001B)\e"\x001B" ใน "\x001B"
\nnnOctal (3 digits)\w\040\w"a b", "c d" ใน "a bc d"
\x nnHex (2 digits)\w\x20\w\w\x20\w
\c XASCII Control (Ctrl-X)\cC"\x0003" ใน "\x0003" (Ctrl-C)
\u nnnnUnicode (4 hex digits)\w\u0020\w"a b", "c d" ใน "a bc d"
\Escape unknown char\d+[\+-x\*]..."2+2", "3*9" ใน "(2+2) * 3*9"

ตัวอย่างการใช้งาน

using System;
using System.Collections.Generic;

class Demo
{
    static void Main()
    {
        Console.WriteLine("Warning!" + '\u0007');
        Console.WriteLine("Demo Text \t Demo Text");
        Console.WriteLine("This is it!\nThis is on the next line!");
    }
}

Output

Warning! Demo Text Demo Text This is it! This is on the next line!