Bảng ký tự thoát và cách hiển thị trong C#:
| Ký tự thoát | Mô tả | Mẫu | Hiển thị |
|---|---|---|---|
| \a | Chuông, \u0007 | \a | "\u0007" trong "Warning!" + '\u0007' |
| \b | Backspace, \u0008 | [\b]{3,} | "\b\b\b\b" trong "\b\b\b\b" |
| \t | Tab, \u0009 | (\w+)\t | "Name\t", "Addr\t" trong "Name\tAddr\t" |
| \r | Carriage return, \u000D | \r\n(\w+) | "\r\nHello" trong "\r\Hello\nWorld." |
| \v | Vertical tab, \u000B | [\v]{2,} | "\v\v\v" trong "\v\v\v" |
| \f | Form feed, \u000C | [\f]{2,} | "\f\f\f" trong "\f\f\f" |
| \n | New line, \u000A | \r\n(\w+) | "\r\nHello" trong "\r\Hello\nWorld." |
| \e | Escape, \u001B | \e | "\x001B" trong "\x001B" |
| \nnn | Octal (tối đa 3 chữ số) | \w\040\w | "a b", "c d" trong "a bc d" |
| \x nn | Hex (đúng 2 chữ số) | \w\x20\w | \w\x20\w |
| \c X\c x | ASCII control (Ctrl-C) | \cC | "\x0003" trong "\x0003" |
| \u nnnn | Unicode 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!