Java examples for java.lang:String Search
color All Word in list of word in Content
//package com.java2s; import java.util.List; public class Main { private static final String TAGBEGIN = "<"; private static final String TAGEND = ">"; public static String colorAllWord(String contentHTML, List<String> listColorWord, List<String> listAllWord, String beginTag, String endTag, String beginNoColorTag, String endNoColorTag) { String string1 = colorWord(contentHTML, listColorWord, beginTag, endTag);/*from w ww. j a v a 2 s. com*/ string1 = colorAllWord(string1, listColorWord, listAllWord, beginNoColorTag, endNoColorTag); return string1; } /** * [Give the description for method]. * @param listColorWord * @param listAllWord * @param beginNoColorTag * @param endNoColorTag * @return */ public static String colorAllWord(String content, List<String> listColorWord, List<String> listAllWord, String beginNoColorTag, String endNoColorTag) { StringBuilder contentHTML = new StringBuilder(content); StringBuilder tempContent = new StringBuilder(content.toUpperCase()); try { int wordIndex = 0; int beginTagIndex = 0; int endTagIndex = 0; String temp = ""; String wordFind = ""; for (String word : listAllWord) { if (!listColorWord.contains(word)) { do { // upper case the word wordFind = word.toUpperCase(); // get next index of word from the previous index wordIndex = tempContent .indexOf(wordFind, wordIndex); if (wordIndex > 0) { char charAfterWord = tempContent .charAt(wordIndex + word.length()); char charBeforeWord = tempContent .charAt(wordIndex - 1); if (!Character.isLetter(charAfterWord) && !Character.isLetter(charBeforeWord)) { // get the next begin tag from the word beginTagIndex = tempContent.indexOf( TAGBEGIN, wordIndex); // get the next end tag from the word endTagIndex = tempContent.indexOf(TAGEND, wordIndex); // if the word is in the content then change word if ((beginTagIndex < endTagIndex) && (beginTagIndex > 0)) { // add tag to tempContent temp = beginNoColorTag + word + endNoColorTag; // tempContent = tempContent.substring(0, wordIndex) + temp // + tempContent.substring(wordIndex + word.length(), tempContent.length()); tempContent .replace(wordIndex, wordIndex + word.length(), temp); // add tag to contentHTML- the return result temp = beginNoColorTag + contentHTML .substring( wordIndex, wordIndex + word.length()) + endNoColorTag; // contentHTML = contentHTML.substring(0, wordIndex) + temp // + contentHTML.substring(wordIndex + word.length(), contentHTML.length()); contentHTML .replace(wordIndex, wordIndex + word.length(), temp); // position for find next occur of the word wordIndex += (beginNoColorTag.length() + endNoColorTag.length() + word .length()); } else { wordIndex = endTagIndex; } } else { wordIndex += word.length(); } } } while (wordIndex > 0); } } } catch (Exception e) { } return contentHTML.toString(); } /** * this method will add tag to all word in contentHTML which is occur in listWord. * @param listWord * @param beginTag * @param endTag * @return String of contentHTML with added tag */ public static String colorWord(String content, List<String> listWord, String beginTag, String endTag) { //String tempContent = contentHTML.toUpperCase(); StringBuilder contentHTML = new StringBuilder(content); StringBuilder tempContent = new StringBuilder(content.toUpperCase()); try { int wordIndex = 0; int beginTagIndex = 0; int endTagIndex = 0; String temp = ""; String wordFind = ""; for (String word : listWord) { do { // upper case the word wordFind = word.toUpperCase(); // get next index of word from the previous index wordIndex = tempContent.indexOf(wordFind, wordIndex); if (wordIndex > 0) { char charAfterWord = tempContent.charAt(wordIndex + word.length()); char charBeforeWord = tempContent .charAt(wordIndex - 1); if (!Character.isLetter(charAfterWord) && !Character.isLetter(charBeforeWord)) { // get the next begin tag from the word beginTagIndex = tempContent.indexOf(TAGBEGIN, wordIndex); // get the next end tag from the word endTagIndex = tempContent.indexOf(TAGEND, wordIndex); // if the word is in the content then change word if ((beginTagIndex < endTagIndex) && (beginTagIndex > 0)) { // add tag to tempContent temp = beginTag + word + endTag; // tempContent = tempContent.substring(0, wordIndex) + temp // + tempContent.substring(wordIndex + word.length(), tempContent.length()); tempContent.replace(wordIndex, wordIndex + word.length(), temp); // add tag to contentHTML- the return result temp = beginTag + contentHTML.substring(wordIndex, wordIndex + word.length()) + endTag; // contentHTML = contentHTML.substring(0, wordIndex) + temp // + contentHTML.substring(wordIndex + word.length(), contentHTML.length()); contentHTML.replace(wordIndex, wordIndex + word.length(), temp); // position for find next occur of the word wordIndex += (beginTag.length() + endTag.length() + word.length()); } else { wordIndex = endTagIndex; } } else { wordIndex += word.length(); } } } while (wordIndex > 0); } } catch (Exception e) { } return contentHTML.toString(); } }