突出显示多个关键字
Highlighting the multiple keywords
我试图突出显示 gridview.I 中的多个关键字,尝试使用 forloop 但它仅突出显示数组中的第一项。
protected string HighlightText(string searchWord, string inputText)
{
// string[] strArray = new string[] { "Hello", "Welcome" };
string s = "d,s";
// Split string on spaces.
// ... This will separate all the words.
string[] words = s.Split(',');
for (int i = 0; i < words.Length; i++)
{
//Console.WriteLine(word);
searchWord = words[i];
Regex expression = new Regex(searchWord.Replace(" ", "|"), RegexOptions.IgnoreCase);
return expression.Replace(inputText, new MatchEvaluator(ReplaceKeywords));
}
return string.Empty;
}
提前致谢。
这是输出 我只得到关键字 "d" 得到突出显示 我还需要突出显示关键字 "s"...
你能试试这样吗,而不是 1 乘 1 地循环搜索关键字
string inputText = "this is keyword1 for test and keyword4 also";
Regex keywords= new Regex("keyword1|keyword2|keyword3|keyword4");
kewyords = kewyords.Replace("|", "\b|\b"); //or use \b between keywords
foreach (Match match in keywords.Matches(inputText))
{
//get match.Index & match.Length for selection and color it
}
我试图突出显示 gridview.I 中的多个关键字,尝试使用 forloop 但它仅突出显示数组中的第一项。
protected string HighlightText(string searchWord, string inputText)
{
// string[] strArray = new string[] { "Hello", "Welcome" };
string s = "d,s";
// Split string on spaces.
// ... This will separate all the words.
string[] words = s.Split(',');
for (int i = 0; i < words.Length; i++)
{
//Console.WriteLine(word);
searchWord = words[i];
Regex expression = new Regex(searchWord.Replace(" ", "|"), RegexOptions.IgnoreCase);
return expression.Replace(inputText, new MatchEvaluator(ReplaceKeywords));
}
return string.Empty;
}
提前致谢。
这是输出 我只得到关键字 "d" 得到突出显示 我还需要突出显示关键字 "s"...
你能试试这样吗,而不是 1 乘 1 地循环搜索关键字
string inputText = "this is keyword1 for test and keyword4 also";
Regex keywords= new Regex("keyword1|keyword2|keyword3|keyword4");
kewyords = kewyords.Replace("|", "\b|\b"); //or use \b between keywords
foreach (Match match in keywords.Matches(inputText))
{
//get match.Index & match.Length for selection and color it
}