当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to print control characters in Console Window

问题描述:

I have tab characters assigned in ASCII (09) or Unicode

char ch = '\x09';

(or)

char ch = '\u0009';

How do I print '\t' in console window ?

None of the below works.(maybe it prints a tab, but not as a canonical representation of '\t')

Console.Write(ch);

Console.Write(ch.ToString())

Guess,Console.Write() is not the right way to do it

网友答案:

Control characters don't display, since that is the whole point of a control character. The reason for putting a tab in a piece of text is to have a tab, after all.

Ideally, we could use the standard symbols ␀␁␂␃␄␅␆␇␈␉␊␋␌␍␎␏␐␑␒␓␔␕␖␗␘␙␚␛␜␝␞␟␠␡␤␥␦ but they don't have great font support (right now as I see it, the symbol for Delete Form Two and Subsitute Form Two aren't showing correctly) and this is even worse with the console.

You're also not clear in your question whether you want the canonical representation (U+0009) or the C# escape (\t) as you ask for one right after asking for the other ("a canonical representation of '\t'").

Assuming the latter, we've an issue in that C# only provides such short-cut escapes for 8 of the control characters. The process will also requires us to escape \ for the same reasons that C# does - how otherwise can we detect whether \t means tab, or means \ followed by t?

So, assuming you want a form that you could then use directly in C# again, we can do the following:

public static class StringEscaper
{
    public static string EscapeForCSharp(this string str)
    {
        StringBuilder sb = new StringBuilder();
        foreach(char c in str)
            switch(c)
            {
                case '\'': case '"': case '\\':
                    sb.Append(c.EscapeForCSharp());
                    break;
                default:
                    if(char.IsControl(c))
                        sb.Append(c.EscapeForCSharp());
                    else
                        sb.Append(c);
                    break;
            }
        return sb.ToString();
    }
    public static string EscapeForCSharp(this char chr)
    {
        switch(chr)
        {//first catch the special cases with C# shortcut escapes.
            case '\'':
                return @"\'";
            case '"':
                return "\\\"";
            case '\\':
                return @"\\";
            case '\0':
                return @"\0";
            case '\a':
                return @"\a";
            case '\b':
                return @"\b";
            case '\f':
                return @"\f";
            case '\n':
                return @"\n";
            case '\r':
                return @"\r";
            case '\t':
                return @"\t";
            case '\v':
                return @"\v";
            default:
                //we need to escape surrogates with they're single chars,
                //but in strings we can just use the character they produce.
                if(char.IsControl(chr) || char.IsHighSurrogate(chr) || char.IsLowSurrogate(chr))
                    return @"\u" + ((int)chr).ToString("X4");
                else
                    return new string(chr, 1);
        }
    }
}

Now we can test it with both a string and a single char.

Single char:

Console.WriteLine('\t'.EscapeForCSharp());

Outputs:

\t

String:

string str = "The following string contains all the \"C0\" and \"C1\" controls, escaped with \\ as per C# syntax: "
  + "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u000A\u000B\u000C\u000D\u000E\u000F\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F\u007F\u0080\u0081\u0082\u0083\u0084\u0085\u0086\u0087\u0088\u0089\u008A\u008B\u008C\u008D\u008E\u008F\u0090\u0091\u0092\u0093\u0094\u0095\u0096\u0097\u0098\u0099\u009A\u009B\u009C\u009D\u009E\u009F";
Console.WriteLine(str.EscapeForCSharp());

Outputs:

The following string contains all the \"C0\" and \"C1\" controls, escaped with \\ as per C# syntax: \0\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n\v\f\r\u000E\u000F\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F\u007F\u0080\u0081\u0082\u0083\u0084\u0085\u0086\u0087\u0088\u0089\u008A\u008B\u008C\u008D\u008E\u008F\u0090\u0091\u0092\u0093\u0094\u0095\u0096\u0097\u0098\u0099\u009A\u009B\u009C\u009D\u009E\u009F
网友答案:

There is no built in way to print characters as C# escape sequences.

Just use dictionary to map characters to strings as needed:

var map = new Dictionary<char, string>{ {'\t', @"\t"}}

And use it to replace characters (if present in map) during output.

网友答案:

Try this:

    string tab = "\u0009";
    Console.Write(tab.Replace(tab, "\\t"));
网友答案:

\t has a special meaning (a tab) so if you want to print it you need to escape the '\'.

Console.Writeline("\tHello World");
prints:     Hello World

Console.Writeline("\\tHello World");
prints: \tHello World

You can also use the @-Syntax to remove the special meanings of \t, \n, \'...

分享给朋友:
您可能感兴趣的文章:
随机阅读: