2

How can I print the multi-colored characters in a one line using only basic commands, such as Console.Write and Console.WriteLine? For example I need something like:

Console.WriteLine($"red{M}green{U}blue{L}yellow{T}purple{I}");

where M, U and L are different values, like int or string.

  • You can make a method which will take parameter like this and do *many* lines of code to [output colored text into console](https://stackoverflow.com/q/2743260/1997232). – Sinatr Jan 16 '19 at 14:01
  • what if, say `M == "black"`, `U == "light"` when `L`, `T` and `l` are empty? you'll have `"redblackgreenlightblueyellowpurple"`. What and why should be colored then? – Dmitry Bychenko Jan 16 '19 at 14:02
  • can you elaborate on the requirement of why you would need to do this in one line? seems like an awful strange requirement. You can make a method which will take in the needed params and do this work for you but it won't be one line... You can make use of `Console.BackgroundColor = ConsoleColor.Blue;` and `Console.ForegroundColor = ConsoleColor.White;` (setting the colors to whatever you need of course – GregH Jan 16 '19 at 14:04
  • Possible duplicate of [Is it possible to write to the console in colour in .NET?](https://stackoverflow.com/questions/2743260/is-it-possible-to-write-to-the-console-in-colour-in-net) – JuanR Jan 16 '19 at 14:12
  • This requirement is for exercise sake, but I'm also trying to check if there are some simple operandors already implemented in C# console that interpret mixed types of variables in a one multi-colored line w/o additional methods. – Stanislav Shustoff Jan 16 '19 at 14:17

3 Answers3

1

You can use a dictionary as shown in below code and then a loop :

Dictionary<int, KeyValuePair<string, ConsoleColor>> keyValuePairs = new Dictionary<int, KeyValuePair<string, ConsoleColor>>();
keyValuePairs.Add(1, new KeyValuePair<string, ConsoleColor>("my blue text", ConsoleColor.Blue));
keyValuePairs.Add(2, new KeyValuePair<string, ConsoleColor>("my red text", ConsoleColor.Red));



foreach (var keyItem in keyValuePairs.Keys)
        {
            ConsoleColor color = keyValuePairs[keyItem].Value;
            string textTobeDisplayed = keyValuePairs[keyItem].Key;

            Console.ForegroundColor = color;
            Console.Write(textTobeDisplayed);
        }
Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37
  • As far as I understood, there are no special tag-like operandors that change the output char's color inside the "Console.Write" commands and all text coloring should be preset in a dictionary... Thank you for the advice. – Stanislav Shustoff Jan 16 '19 at 14:35
1

This is what I would use, probably not the best method but it does work.

class Program
{
    static void Main(string[] args)
    {
        // ~ red, ` green, ^ blue, * yellow, _ purple
        ColorWrite("~M`u^l*t_i", true);
        ColorWrite("~This is red,^ yet this is blue.", true);
        ColorWrite("~Mul`ti^ple ", false);
        ColorWrite("*Col_ours ", false);

    }

    // variable contains both consoleColor and char
    struct ColourKey
    {
        public ConsoleColor color;
        public char key;

        public ColourKey(ConsoleColor Color, char Key)
        {
            this.color = Color;
            this.key = Key;
        }
    }

    static void ColorWrite(string rawtext, bool endline)
    {
        //all avaliable colours, for more just make the array bigger
        ColourKey[] Pallete = new ColourKey[5];
        Pallete[0] = new ColourKey(ConsoleColor.Red, '~');
        Pallete[1] = new ColourKey(ConsoleColor.Green, '`');
        Pallete[2] = new ColourKey(ConsoleColor.Blue, '^');
        Pallete[3] = new ColourKey(ConsoleColor.Yellow, '*');
        Pallete[4] = new ColourKey(ConsoleColor.DarkMagenta, '_'); //ConsoleColor does not contain purple

        foreach (char c in rawtext)
        {
            bool CanWrite = true;
            foreach (ColourKey ck in Pallete)
            {
                if (c == ck.key)
                {
                    Console.ForegroundColor = ck.color;
                    CanWrite = false;
                }
            }

            if (CanWrite)
            {
                Console.Write(c);
            }

        }

        Console.ResetColor();

        // true function works like writeline(), false works like write()
        if (endline)
        {
            Console.WriteLine();
        }
        
    }
-1

I hope this help you

class Program
{
    static void Main(string[] args)
    {
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.Write("White on Blue.");
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write("White on Red.");
        Console.ResetColor();
        Console.ReadLine();
    }
}
  • This is obvious. But couldn't it be consolidated into one line somehow, w/o code breakpoints ";"? MB there are some text styling tools that avoid changing the whole console coloring? – Stanislav Shustoff Jan 16 '19 at 14:23
  • I mean, when you have to print the text where all letters are changing colors w/o special method the code grows like darn mushroom after the rain – Stanislav Shustoff Jan 16 '19 at 14:26