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" 中的 "\r\Hello
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" 中的 "\r\Hello
World."
\e匹配轉義符,\u001B。\e"\x001B" 中的 "\x001B"

nn
使用八進位制表示法指定字元(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!

更新於: 2020-06-20

827 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.