Java tutorial
//package com.java2s; public class Main { public static String toHTMLString(String in) { StringBuffer out = new StringBuffer(); for (int i = 0; in != null && i < in.length(); i++) { char c = in.charAt(i); if (c == '\'') out.append("'"); else if (c == '\"') out.append("""); else if (c == '<') out.append("<"); else if (c == '>') out.append(">"); else if (c == '&') out.append("&"); else if (c == ' ') out.append(" "); else if (c == '\n') out.append("<br/>"); else out.append(c); } return out.toString(); } }