Java tutorial
//package com.java2s; //License from project: Apache License import java.text.CharacterIterator; import java.text.StringCharacterIterator; public class Main { static String escape(String input) { if (input == null) { return null; } StringBuilder result = new StringBuilder(); StringCharacterIterator iterator = new StringCharacterIterator(input); char character = iterator.current(); while (character != CharacterIterator.DONE) { if (character == '<') { result.append("<"); } else if (character == '>') { result.append(">"); } else if (character == '\"') { result.append("""); } else if (character == '\'') { result.append("'"); } else if (character == '&') { result.append("&"); } else { result.append(character); } character = iterator.next(); } return result.toString(); } }