2017-05-06 10 views
0

Iloがforloopを試してみましたが、gridview.Iで複数のキーワードを強調表示しようとしましたが、配列の最初の項目のみを強調表示しています。複数のキーワードを強調表示

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」を取得Iamは入れていた私は、「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

ありがとうございました。私は答えを得ました、私はちょうどキーワード文字列から私のコンマを変更し、パイプシンボルに変更して、それは私のためにうまくいく。あなたの偉大なアイデアをお寄せいただきありがとうございます。 –

関連する問題