Java - Write code to quote Html Special Chars

Requirements

Write code to quote Html Special Chars

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String html = "<>book2s.com";
        System.out.println(quoteHtmlSpecialChars(html));
    }//ww  w  .jav  a2s.c  o m

    public static String quoteHtmlSpecialChars(String html) {
        if (html == null)
            return null;
        String[] specialChars = { "&", "\"", "'", "<", ">" };
        String[] quoteChars = { "&amp;", "&quot;", "&apos;", "&lt;", "&gt;" };
        for (int i = 0; i < specialChars.length; i++) {
            html = html.replace(specialChars[i], quoteChars[i]);
        }
        return html;
    }
}