首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Colorize C#控制台无法正常工作

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

Stack Overflow用户
提问于 2013-01-27 00:28:15
回答 2查看 400关注 0票数 0

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

代码语言:javascript
复制
    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!");

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-27 00:53:47

你是对的,Regex.Matches和Regex.Split的结合让事情变得有点尴尬,所以我把它们组合在一起

代码语言:javascript
复制
    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;
    }
票数 1
EN

Stack Overflow用户

发布于 2013-01-27 00:40:48

唯一的解决方法是

代码语言:javascript
复制
var i = 1;

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

代码语言:javascript
复制
    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;
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14538955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档