Here you can find the source of toHtmlString(String s)
public static String toHtmlString(String s)
//package com.java2s; public class Main { public static final String BOLD_START = "%%%BOLD_START%%%"; public static final String BOLD_END = "%%%BOLD_END%%%"; public static final String NEW_LINE = "%%%NEW_LINE%%%"; public static String toHtmlString(String s) { String toReturn = null;/* ww w .j av a2 s .co m*/ if (s == null) { return null; } if (s.contains("!DOCTYPE HTML")) { s = replace(s, "!DOCTYPE HTML", ""); return s; } toReturn = s; if (toReturn.contains("<")) { if (toReturn.contains("</a>") || toReturn.contains("</A>")) { return toReturn; } toReturn = replace(toReturn, "<B>", BOLD_START); toReturn = replace(toReturn, "<b>", BOLD_START); toReturn = replace(toReturn, "</B>", BOLD_END); toReturn = replace(toReturn, "</b>", BOLD_END); toReturn = replace(toReturn, "<BR>", NEW_LINE); toReturn = replace(toReturn, "<br>", NEW_LINE); toReturn = replace(toReturn, "<", "<"); toReturn = replace(toReturn, ">", ">"); toReturn = replace(toReturn, BOLD_START, "<B>"); toReturn = replace(toReturn, BOLD_END, "</B>"); toReturn = replace(toReturn, NEW_LINE, "<br>"); } toReturn = replace(toReturn, "\r\n", "<br>"); //toReturn = replace(toReturn, "\n\r", "<br>"); toReturn = replace(toReturn, "\r", "<br>"); toReturn = replace(toReturn, "\n", "<br>"); toReturn = replace(toReturn, " ", " "); toReturn = replace(toReturn, "\t", " "); return toReturn; } /** * The source was taken from the internet. Replace a text in other text in a * given input text. * * @param lookIn * The text to look in. * @param lookFor * The text to look for. * @param replaceWith * The text to replace with. * * @return The text after the replace operation. */ public static String replace(String lookIn, String lookFor, String replaceWith) { int count = 0; int i, j; StringBuffer sb; for (i = 0; (i = lookIn.indexOf(lookFor, i)) != -1; i += lookFor.length()) ++count; if (count == 0) { return lookIn; } sb = new StringBuffer(lookIn.length() + count * (replaceWith.length() - lookFor.length())); for (i = 0; (j = lookIn.indexOf(lookFor, i)) != -1; i = j + lookFor.length()) sb.append(lookIn.substring(i, j)).append(replaceWith); sb.append(lookIn.substring(i)); return sb.toString(); } }