Skip to content

Instantly share code, notes, and snippets.

@gubatron
Last active July 20, 2018 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gubatron/baafb7ff071aa7f5d7b134b6bb1af5d7 to your computer and use it in GitHub Desktop.
Save gubatron/baafb7ff071aa7f5d7b134b6bb1af5d7 to your computer and use it in GitHub Desktop.
Java implementation of HTML highlighter. Replaces found query tokens in target strings for their HTML bolded version, keeping the original case of the token.
static String simpleHTMLHighlighter(String query, String str) {
if (query == null || query.isEmpty()) {
return str;
}
// Get unique tokens in the query
String[] rawTokens = query.split("\\s+");
HashSet<String> uniqueTokens = new HashSet<>();
for (String token : rawTokens) {
uniqueTokens.add(token.trim());
}
// Replace all instances of unique tokens for bolded versions in original string
for (String token : uniqueTokens) {
// find case insensitive regions where our token could be
int offset=0;
int regionEnd = offset + token.length();
while (regionEnd < str.length()) {
if (str.regionMatches(true, offset, token, 0, token.length())) {
String tokenAsFoundInStr = str.substring(offset, offset + token.length());
str = str.substring(0, offset) + "<b>" + tokenAsFoundInStr + "</b>" +
str.substring(offset + token.length());
offset += token.length() + 7;
} else {
offset += 1;
}
regionEnd = offset + token.length();
}
}
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment