Last active
July 20, 2018 02:36
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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