2016-07-26 46 views
0

我想要一個用戶輸入值,我將其保存到一個字符串中,然後將該字符串插入到控制檯輸出中。有沒有辦法讓插入的字符串改變顏色。我知道使用Console.Background/ForegroundColor,但到目前爲止,這些已經改變了整個輸出的顏色。 最好的幫助是,如果有一些方法來利用代碼類似於如何更改插值字符串的顏色?

Console.WriteLine($"This would be the {string}."

,並有串{}不同的顏色,但我將納入任何作品。

+0

你有沒有考慮使用Console.Write()代替(沒有換行符在每次通話結束),只是分離的字符串轉換成單獨的呼叫? – redunderthebed

+1

寫入特定顏色文字後使用resetcolor ....... http://www.dotnetperls.com/console-color –

回答

0

希望這將做的伎倆爲您

string letters = $"This would be the {string}." 
string ColoredLetters = {string}; // Whatever is your string 
Char[] array = letters.ToCharArray(); 
void WriteLineWithColoredLetter(string letters, char c) 
{ 
    var NormalWrite = letters.IndexOf(c); 
    Console.Write(letters.Substring(0, NormalWrite)); 
    Console.ForegroundColor = ConsoleColor.Red; 
    Console.Write(ColoredLetters); 
    Console.ResetColor(); 
    Console.WriteLine(letters.Substring(NormalWrite + 1)); 
}