SuggestionBase implements Suggestion { protected String highlight(String text, String hint, String delimiter) { if (StringUtils.isBlank(text) || StringUtils.isBlank(delimiter)) return text; String lowercasedText = text.toLowerCase(); int position = lowercasedText.indexOf(hint); if (position >= 0) { StringBuffer result = new StringBuffer(); int startOfRest = 0; while (position >= 0) { result.append(text.substring(startOfRest, position)); result.append("<").append(delimiter).append(">"); result.append(text.substring(position, position + hint.length())); result.append("</").append(delimiter).append(">"); startOfRest = position + hint.length(); position = lowercasedText.indexOf(hint, startOfRest); } result.append(text.substring(startOfRest)); return result.toString(); } else { return text; } }    }
@Test public void testHighlight() { assertEquals(highlightedText, suggestion.highlight(text, hint, delimiter)); assertEquals(text, suggestion.highlight(text, hint, null)); assertNull(suggestion.highlight(null, hint, delimiter)); }