Colorize C#控制台无法正常工作

时间:2013-01-26 16:28:15

标签: c# console colorize

我需要通过方法对字符串进行着色,所以我使用Console.ForegroundColor属性并稍后写文本,但是我在某个地方犯了一个错误,所以所有的行都是彩色的。或者有更好的解决方案吗?我需要通过& 0-& f(hex)将字符串着色并输出到控制台,这是我的解决方案:

    public static void ColorizeConsoleMessage(string message)
    {
        var matches = Regex.Matches(message, "&+[0-9a-f]");
        var split = Regex.Split(message, "&+[0-9a-f]");
        var def = Console.ForegroundColor;
        var i = 0;
        foreach (var match in matches)
        {
            switch (match.ToString().Replace("&", "").ToCharArray()[0])
            {
                case '0':
                    Console.ForegroundColor = ConsoleColor.White;
                    break;
                case '1':
                    Console.ForegroundColor = ConsoleColor.Gray;
                    break;
                case '2':
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                    break;
                case '3': 
                    Console.ForegroundColor = ConsoleColor.Black;
                    break;
                case '4':
                    Console.ForegroundColor = ConsoleColor.Red;
                    break;
                case '5':
                    Console.ForegroundColor = ConsoleColor.Green;
                    break;
                case '6':
                    Console.ForegroundColor = ConsoleColor.Blue;
                    break;
                case '7':
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    break;
                default:
                    Console.ForegroundColor = ConsoleColor.White;
                    break;
            }
            Console.Write(split[i]);
            i++;
        }
        Console.WriteLine();
        Console.ForegroundColor = def;
    }

并测试:EventManager.ColorizeConsoleMessage("&4Hello, &6world!");

2 个答案:

答案 0 :(得分:2)

唯一的解决办法是

var i = 1;

Regex.Split在split []中创建一个空字符串元素,这会弄乱所有索引值

    public static void ColorizeConsoleMessage(string message)
    {
        MatchCollection matches = Regex.Matches(message, "&+[0-9a-f]");
        string[] split = Regex.Split(message, "&+[0-9a-f]");
        ConsoleColor def = Console.ForegroundColor;
        int i = 1;
        foreach (Match match in matches)
        {
            switch (match.Value[1])
            {
                case '0':
                    Console.ForegroundColor = ConsoleColor.White;
                    break;
                case '1':
                    Console.ForegroundColor = ConsoleColor.Gray;
                    break;
                case '2':
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                    break;
                case '3':
                    Console.ForegroundColor = ConsoleColor.Black;
                    break;
                case '4':
                    Console.ForegroundColor = ConsoleColor.Red;
                    break;
                case '5':
                    Console.ForegroundColor = ConsoleColor.Green;
                    break;
                case '6':
                    Console.ForegroundColor = ConsoleColor.Blue;
                    break;
                case '7':
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    break;
                default:
                    Console.ForegroundColor = ConsoleColor.White;
                    break;
            }
            Console.Write(split[i]);
            i++;
        }
        Console.WriteLine();
        Console.ForegroundColor = def;
    }

答案 1 :(得分:1)

你是正确的Regex.Matches& Regex.Split一起让事情有点尴尬,所以我把它们结合起来

    public static void ColorizeConsoleMessage(string message)
    {
        MatchCollection matches = Regex.Matches(message, "&+([0-9a-f])([^&]+)");
        ConsoleColor def = Console.ForegroundColor;
        foreach (Match match in matches)
        {
            switch (match.Groups[1].Value[0])
            {
                case '0':
                    Console.ForegroundColor = ConsoleColor.White;
                    break;
                case '1':
                    Console.ForegroundColor = ConsoleColor.Gray;
                    break;
                case '2':
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                    break;
                case '3':
                    Console.ForegroundColor = ConsoleColor.Black;
                    break;
                case '4':
                    Console.ForegroundColor = ConsoleColor.Red;
                    break;
                case '5':
                    Console.ForegroundColor = ConsoleColor.Green;
                    break;
                case '6':
                    Console.ForegroundColor = ConsoleColor.Blue;
                    break;
                case '7':
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    break;
                default:
                    Console.ForegroundColor = ConsoleColor.White;
                    break;
            }
            string str_to_print = match.Groups[2].Value;
            Console.Write(str_to_print);
        }
        Console.WriteLine();
        Console.ForegroundColor = def;
    }