Tah*_*mid 1 vb.net colors console-application
因此,我正在Visual Basic中基于控制台的应用程序工作,但遇到了问题。我正在尝试向控制台添加颜色,但只能在行内添加1个单词。我知道Console.ForegroundColor = ConsoleColor.Red
选项,但是颜色是整行而不是行中的1个字。我将在下面提供一些示例。
这是一些示例代码:
'If I use it like this the whole line will turn red
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine("Hello stackoverflow, I need some help!")
Run Code Online (Sandbox Code Playgroud)
如上所述,整行变成红色。如果我只希望单词“ stackoverflow”为红色,而句子的其余部分保持正常颜色怎么办?
是否有可能做到这一点?
提前致谢。
Console.Write("Hello ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("stackoverflow");
Console.ResetColor();
Console.WriteLine(", I need some help!");
Run Code Online (Sandbox Code Playgroud)
您可能希望对字符串进行标记化,并使用某种模式匹配功能来构建可重用的内容。
为字符串中的单个单词上色(添加逻辑以处理逗号和句点):
private static void colorize(string expression, string word)
{
string[] substrings = expression.Split();
foreach (string substring in substrings)
{
if (substring.Contains(word))
{
Console.ForegroundColor = ConsoleColor.Red;
}
Console.Write(substring+" ");
Console.ResetColor();
}
Console.WriteLine();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9611 次 |
最近记录: |