2017-05-06 186 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得到了答案,我只是從關鍵字字符串更改我的逗號,並將其更改爲管道符號,那麼它對我來說工作正常。感謝您的好主意,我從你的答案中得到了這個想法。 –