2016-08-08 6 views
1

アンドロイドには、メインの文字列と、スペースで区切られたキーワードを持つ別の文字列があります。 一致するキーワードを持つメインの文字列が強調表示されている文字列を出力したい。Androidの部分文字列のキーワードの一致

Ex。

String strMain = "This string contains  the main contents."; 

String strSearchKeywords = "this cont"; 

出力結果のようなものでなければならない:

String strResult = "<b>This</b> string <b>cont</b>ains  the main <b>cont</b>ents."; 

一つの方法は、配列リストに検索キーワードを分割して、各項目を反復処理し、メインのコンテンツを強調することです。

しかし、regex matchを使うのと同じように、反復処理をしなくても他の方法がありますか?

ありがとうございました。

strMain = strMain .replaceAll("(?i)(this|cont)", 
     "<b>$1</b>"); 

(?i)を意味

答えて

3
strMain = strMain .replaceAll("(?i)(" + strSearchKeywords.replace(' ', '|') + ")", 
     "<b>$1</b>"); 

無視ケースのフラグを設定します。

関連する問題