title: Highlight multiple words in a string using SpannableString
body: <p>I'm using a <code>SpannableString</code> to underline certain words, however, I realized the code I have only highlights the first word if there are multiple words.  Not exactly sure how to accomplish highlighting multiple words:</p>
  
  <pre><code>String keyword = "test";
  String text = "This is a test to underline the three test words in this test";
  
  SpannableString output = new SpannableString(text);
  
  if (text.indexOf(keyword) &gt; -1)
  {
      int keywordIndex = text.indexOf(keyword);
      int keywordLength = keyword.length();
  
      int start = keywordIndex;
      int end = keywordIndex + (keywordLength);                   
  
      output.setSpan(new UnderlineSpan(), start, end, 0);
  }
  </code></pre>
  
  <p>I was thinking I could <code>split</code> the text at every space and loop through it, but wasn't sure if there was a better way.</p>
  
  <p>I do have this code to highlight multiple words using a regular expression, however, I'm try to avoid regular expressions since it's in an Android app and I'm using it in a <code>ListView</code> and I'm told they are very expensive.  Also this code I have only highlight whole words, so using the example text above, if the word "protest" was in the sentence, it wouldn't get highlighted using this code:</p>
  
  <pre><code> Matcher matcher = Pattern.compile("\\b(?:test")\\b").matcher(text);
  
  while (matcher.find())
  {
    output.setSpan(new UnderlineSpan(), matcher.start(), matcher.end(), 0);
  }
  </code></pre>