我试图在gridview中突出显示多个关键字。我尝试使用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”也...
答案 0 :(得分:1)
你可以尝试这样的事情,而不是循环使用关键字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
}