@Test public void testApplyBoldFragments_MatchAtEnd() throws Exception { Configurations.currentConfiguration = new Configuration(); String expected = "<html>hello world<b>abc</b></html>"; assertThat(HtmlUtils.applyBoldFragments("abc","hello worldabc")).isEqualTo(expected); }
public static String applyBoldFragments(String fragment, String sourceText) { if (Strings.isNullOrEmpty(fragment) || Strings.isNullOrEmpty(sourceText)) { return "<html>" + sourceText + "</html>"; } String lowerFragment = fragment.toLowerCase(); String lowerSource = sourceText.toLowerCase(); int sourceIndex = 0; int matchIndex; StringBuilder sb = new StringBuilder("<html>"); do { matchIndex = lowerSource.indexOf(lowerFragment, sourceIndex); if (matchIndex > -1) { sb.append(sourceText.substring(sourceIndex, matchIndex)) .append("<b>") .append(sourceText.substring(matchIndex, matchIndex + fragment.length())) .append("</b>"); sourceIndex = matchIndex + fragment.length(); } } while (matchIndex > -1); sb.append(sourceText.substring(sourceIndex)); sb.append("</html>"); return sb.toString(); }
HtmlUtils { public static String applyBoldFragments(String fragment, String sourceText) { if (Strings.isNullOrEmpty(fragment) || Strings.isNullOrEmpty(sourceText)) { return "<html>" + sourceText + "</html>"; } String lowerFragment = fragment.toLowerCase(); String lowerSource = sourceText.toLowerCase(); int sourceIndex = 0; int matchIndex; StringBuilder sb = new StringBuilder("<html>"); do { matchIndex = lowerSource.indexOf(lowerFragment, sourceIndex); if (matchIndex > -1) { sb.append(sourceText.substring(sourceIndex, matchIndex)) .append("<b>") .append(sourceText.substring(matchIndex, matchIndex + fragment.length())) .append("</b>"); sourceIndex = matchIndex + fragment.length(); } } while (matchIndex > -1); sb.append(sourceText.substring(sourceIndex)); sb.append("</html>"); return sb.toString(); } }
HtmlUtils { public static String applyBoldFragments(String fragment, String sourceText) { if (Strings.isNullOrEmpty(fragment) || Strings.isNullOrEmpty(sourceText)) { return "<html>" + sourceText + "</html>"; } String lowerFragment = fragment.toLowerCase(); String lowerSource = sourceText.toLowerCase(); int sourceIndex = 0; int matchIndex; StringBuilder sb = new StringBuilder("<html>"); do { matchIndex = lowerSource.indexOf(lowerFragment, sourceIndex); if (matchIndex > -1) { sb.append(sourceText.substring(sourceIndex, matchIndex)) .append("<b>") .append(sourceText.substring(matchIndex, matchIndex + fragment.length())) .append("</b>"); sourceIndex = matchIndex + fragment.length(); } } while (matchIndex > -1); sb.append(sourceText.substring(sourceIndex)); sb.append("</html>"); return sb.toString(); } private  HtmlUtils(); }
HtmlUtils { public static String applyBoldFragments(String fragment, String sourceText) { if (Strings.isNullOrEmpty(fragment) || Strings.isNullOrEmpty(sourceText)) { return "<html>" + sourceText + "</html>"; } String lowerFragment = fragment.toLowerCase(); String lowerSource = sourceText.toLowerCase(); int sourceIndex = 0; int matchIndex; StringBuilder sb = new StringBuilder("<html>"); do { matchIndex = lowerSource.indexOf(lowerFragment, sourceIndex); if (matchIndex > -1) { sb.append(sourceText.substring(sourceIndex, matchIndex)) .append("<b>") .append(sourceText.substring(matchIndex, matchIndex + fragment.length())) .append("</b>"); sourceIndex = matchIndex + fragment.length(); } } while (matchIndex > -1); sb.append(sourceText.substring(sourceIndex)); sb.append("</html>"); return sb.toString(); } private  HtmlUtils(); static String localiseWithLineBreaks(String[] lines); static String localiseWithCenteredLinedBreaks(String[] lines); static String applyBoldFragments(String fragment, String sourceText); static String stripHtml(String html); }
HtmlUtils { public static String applyBoldFragments(String fragment, String sourceText) { if (Strings.isNullOrEmpty(fragment) || Strings.isNullOrEmpty(sourceText)) { return "<html>" + sourceText + "</html>"; } String lowerFragment = fragment.toLowerCase(); String lowerSource = sourceText.toLowerCase(); int sourceIndex = 0; int matchIndex; StringBuilder sb = new StringBuilder("<html>"); do { matchIndex = lowerSource.indexOf(lowerFragment, sourceIndex); if (matchIndex > -1) { sb.append(sourceText.substring(sourceIndex, matchIndex)) .append("<b>") .append(sourceText.substring(matchIndex, matchIndex + fragment.length())) .append("</b>"); sourceIndex = matchIndex + fragment.length(); } } while (matchIndex > -1); sb.append(sourceText.substring(sourceIndex)); sb.append("</html>"); return sb.toString(); } private  HtmlUtils(); static String localiseWithLineBreaks(String[] lines); static String localiseWithCenteredLinedBreaks(String[] lines); static String applyBoldFragments(String fragment, String sourceText); static String stripHtml(String html);  }