gpt4 book ai didi

c# - 有没有一种方法可以搜索用户从文件中加载的文本中的特定单词并为其着色?

转载 作者:太空宇宙 更新时间:2023-11-03 22:33:44 24 4
gpt4 key购买 nike

用户指定了一个文件路径,然后将所有可读内容插入到一个字符串中,但我现在在搜索该字符串以查找用户输入的单词时遇到了问题,如果出现匹配项,我还无法为其着色。

 Console.WriteLine("Please enter the path of the document to be searched");

do
{

text = Helper.TryToReadFile();
} while (text == null);

Console.WriteLine("Choosen Text:\n---------------------------------------\n");
Console.WriteLine(text);
Console.WriteLine("---------------------------------------\n");
//
Console.WriteLine("Would you like to search for whole words ?");
bool decisionForWholeWords = Helper.ConvertToBool(Console.ReadLine());

Console.WriteLine("Should the spelling be observed ?");
bool decisionForSpelling = Helper.ConvertToBool(Console.ReadLine());

最佳答案

一个简单的基本逻辑

  public static void Main(string[] args)
{
string txt = "this is a an example string";
string wordToFind = "example string";
bool phraseFound = false;
var splittedTxt = txt.Split(' ');
var wordToFindList = wordToFind.Split(' ');
foreach (var item in splittedTxt)
{
if (wordToFindList.Any(item.Contains))
{
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
phraseFound = true;
}
Console.Write(item);
if (phraseFound) // reset color
{
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Gray;
phraseFound = false;
}
Console.Write(" ");
}


Console.ReadLine();
}

示例输出 enter image description here

关于c# - 有没有一种方法可以搜索用户从文件中加载的文本中的特定单词并为其着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56148246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com