Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Cách in ký tự thoát (Escape Characters) trong C#

Bảng ký tự thoát và cách hiển thị trong C#:

Ký tự thoátMô tảMẫuHiển thị
\aChuông, \u0007\a"\u0007" trong "Warning!" + '\u0007'
\bBackspace, \u0008[\b]{3,}"\b\b\b\b" trong "\b\b\b\b"
\tTab, \u0009(\w+)\t"Name\t", "Addr\t" trong "Name\tAddr\t"
\rCarriage return, \u000D\r\n(\w+)"\r\nHello" trong "\r\Hello\nWorld."
\vVertical tab, \u000B[\v]{2,}"\v\v\v" trong "\v\v\v"
\fForm feed, \u000C[\f]{2,}"\f\f\f" trong "\f\f\f"
\nNew line, \u000A\r\n(\w+)"\r\nHello" trong "\r\Hello\nWorld."
\eEscape, \u001B\e"\x001B" trong "\x001B"
\nnnOctal (tối đa 3 chữ số)\w\040\w"a b", "c d" trong "a bc d"
\x nnHex (đúng 2 chữ số)\w\x20\w\w\x20\w
\c X\c xASCII control (Ctrl-C)\cC"\x0003" trong "\x0003"
\u nnnnUnicode hex (4 chữ số)\w\u0020\w"a b", "c d" trong "a bc d"
\Ký tự không phải escape\d+[\+-x\*]..."2+2", "3*9" trong "(2+2) * 3*9"

Ví dụ

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!");
    }
}

Kết quả

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