Java tutorial
//package com.java2s; public class Main { public static String normalize(final String s) { if (s == null) { return ""; } final StringBuffer str = new StringBuffer(); final int len = s.length(); for (int i = 0; i < len; i++) { final char ch = s.charAt(i); switch (ch) { case '<': { str.append("<"); break; } case '>': { str.append(">"); break; } case '&': { str.append("&"); break; } case '"': { str.append("""); break; } case '\n': { if (i > 0) { str.charAt(str.length() - 1); str.append("\n"); } else { str.append("\n"); } break; } default: { str.append(ch); } } } return (str.toString()); } }