C#中列印跳脫字元的方法
以下是C#中的跳脫字元,顯示列建議瞭如何在C#中使用和列印它們:
跳脫字元 | 描述 | 模式 | 顯示 |
---|---|---|---|
\a | 匹配鈴聲字元,\u0007。 | \a | "Warning!" + '\u0007' 中的 "\u0007" |
\b | 在字元類中,匹配退格符,\u0008。 | [\b]{3,} | "\b\b\b\b" 中的 "\b\b\b\b" |
\t | 匹配製表符,\u0009。 | (\w+)\t | "Name\t", "Addr\t" 在 "Name\tAddr\t" 中 |
\r | 匹配回車符,\u000D。(\r 不等同於換行符, .) | \r (\w+) | "\r Hello" 在 "\rHello World." 中 |
\v | 匹配垂直製表符,\u000B。 | [\v]{2,} | "\v\v\v" 中的 "\v\v\v" |
\f | 匹配換頁符,\u000C。 | [\f]{2,} | "\f\f\f" 中的 "\f\f\f" |
匹配換行符,\u000A。 | \r (\w+) | "\r Hello" 在 "\rHello World." 中 | |
\e | 匹配轉義符,\u001B。 | \e | "\x001B" 中的 "\x001B" |
\nnn | 使用八進位制表示法指定字元(nnn最多包含三位數字)。 | \w\040\w | "a b", "c d" 在 "a bc d" 中 |
\x nn | 使用十六進位制表示法指定字元(nn正好包含兩位數字)。 | \w\x20\w | \w\x20\w |
\c X\c x | 匹配由X或x指定的ASCII控制字元,其中X或x是控制字元的字母。 | \cC | "\x0003" 中的 "\x0003" (Ctrl-C) |
\u nnnn | 使用十六進位制表示法匹配Unicode字元(正好四位數字,如nnnn所示)。 | \w\u0020\w | "a b", "c d" 在 "a bc d" 中 |
\ | 如果後跟未識別為跳脫字元的字元,則匹配該字元。 | \d+[\+-x\*]\d+\d+[\+-x\*\d+ | "2+2" 和 "3*9" 在 "(2+2) * 3*9" 中 |
以下是一個示例,展示如何在C#中使用一些跳脫字元:
示例
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!
This is on the next line!"); } }
輸出
Warning! Demo Text Demo Text This is it! This is on the next line!
廣告