0

文字列をメソッドで色付けする必要があるので、Console.ForegroundColorプロパティを使用して後でテキストを書き込みますが、どこかで間違えたため、すべての行が1色になっています。または、より良い解決策はありますか?文字列を&0-&f(16進数)で色付けしてコンソールに出力する必要があります。これが私の解決策です。

    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!");

4

2 に答える 2

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;
    }
于 2013-01-26T16:40:48.080 に答える
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;
    }
于 2013-01-26T16:53:46.840 に答える