2017-05-06 108 views
0

Iam尝试突出显示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”也...

enter image description here

+1

循环内部的'return'在第一次迭代中终止其执行。 –

回答

0

可以尝试,而不是循环这样的事情,对于关键字1 by 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 
} 
+1

Thanks.I得到了答案,我只是从关键字字符串更改我的逗号,并将其更改为管道符号,那么它对我来说工作正常。感谢您的好主意,我从你的答案中得到了这个想法。 –