Java tutorial
//package com.java2s; // modify it under the terms of the GNU General Public License public class Main { public static String escapeAttribute(String value) { if (value == null) return ""; StringBuffer result = new StringBuffer(value.length()); char[] chars = value.toCharArray(); for (int i = 0; i < chars.length; i++) { switch (chars[i]) { case '<': result.append("<"); break; case '>': result.append(">"); break; case '&': result.append("&"); break; case '"': result.append("""); break; case '\'': result.append("'"); break; default: if (chars[i] < 32) result.append("&#").append((int) chars[i]).append(";"); else result.append(chars[i]); } } return result.toString(); } }