Java tutorial
//package com.java2s; public class Main { public static String encode(Object string) { if (string == null) { return ""; } char[] chars = string.toString().toCharArray(); StringBuffer out = new StringBuffer(); for (int i = 0; i < chars.length; i++) { switch (chars[i]) { case '&': out.append("&"); break; case '<': out.append("<"); break; case '>': out.append(">"); break; case '\"': out.append("""); break; default: out.append(chars[i]); } } return out.toString(); } }