To my knowledge, there are no such control codes in Windows console applications such as cmd.exe. There are some creative ways to achieve a similar result. One of these is How to echo with different colors in the Windows command line. I tried it out of curiosity and it works just fine. It uses some jscript magic. For everyday use you might find one of the bash shell emulators more useful if you want escape code formatting capabilities. (How to develop in Linux-Like Shell (bash) on Windows?)
UPDATE:
I threw together something very quick and dirty to demonstrate one approach to using "codes" in a style similar to one you used in the question. This may not be the "best" way. But it might spark an idea.
class Program
{
static void Main(string[] args)
{
@"
This is in ~r~red~~ and this is in ~b~blue~~. This is just some more text
to work it out a bit. ~g~And now a bit of green~~.
".WriteToConsole();
Console.ReadKey();
}
}
static public class StringConsoleExtensions
{
private static readonly Dictionary<string, ConsoleColor> ColorMap = new Dictionary<string, ConsoleColor>
{
{ "r", ConsoleColor.Red },
{ "b", ConsoleColor.Blue },
{ "g", ConsoleColor.Green },
{ "w", ConsoleColor.White },
};
static public void WriteToConsole(this string value)
{
var position = 0;
foreach (Match match in Regex.Matches(value, @"~(r|g|b|w)~([^~]*)~~"))
{
var leadingText = value.Substring(position, match.Index - position);
position += leadingText.Length + match.Length;
Console.Write(leadingText);
var currentColor = Console.ForegroundColor;
try
{
Console.ForegroundColor = ColorMap[match.Groups[1].Value];
Console.Write(match.Groups[2].Value);
}
finally
{
Console.ForegroundColor = currentColor;
}
}
if (position < value.Length)
{
Console.Write(value.Substring(position, value.Length - position));
}
}
}
I think there may be a way to get the regex to capture the leading text. But I didn't have a lot of time to experiment. I'd be interested to see if there is a pattern that would allow the regex to do all the work.