Java HTML Encode htmlEncode(String strSrc)

Here you can find the source of htmlEncode(String strSrc)

Description

html Encode

License

Open Source License

Declaration


public static String htmlEncode(String strSrc) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {

    public static String htmlEncode(String strSrc) {
        if (strSrc == null) {
            return "";
        }//from  www .  ja  v  a2  s. c o m
        char[] arrCsrc = strSrc.toCharArray();
        StringBuilder buf = new StringBuilder(arrCsrc.length);
        char ch;
        for (char anArrCSrc : arrCsrc) {
            ch = anArrCSrc;

            if (ch == '<') {
                buf.append("&lt;");
            } else if (ch == '>') {
                buf.append("&gt;");
            } else if (ch == '"') {
                buf.append("&quot;");
            } else if (ch == '\'') {
                buf.append("&#039;");
            } else if (ch == '&') {
                buf.append("&amp;");
            } else {
                buf.append(ch);
            }
        }

        return buf.toString();
    }

    public static String htmlEncode(String strSrc, int quotes) {

        if (strSrc == null) {
            return "";
        }
        if (quotes == 0) {
            return htmlEncode(strSrc);
        }

        char[] arrCsrc = strSrc.toCharArray();
        StringBuilder buf = new StringBuilder(arrCsrc.length);
        char ch;

        for (int i = 0; i < arrCsrc.length; i++) {
            ch = arrCsrc[i];
            if (ch == '<') {
                buf.append("&lt;");
            } else if (ch == '>') {
                buf.append("&gt;");
            } else if (ch == '"' && quotes == 1) {
                buf.append("&quot;");
            } else if (ch == '\'' && quotes == 2) {
                buf.append("&#039;");
            } else if (ch == '&') {
                buf.append("&amp;");
            } else {
                buf.append(ch);
            }
        }

        return buf.toString();
    }
}

Related

  1. htmlEncode(String string)
  2. HTMLEncode(String string)
  3. htmlEncode(String string)
  4. HtmlEncode(String strInput)
  5. htmlEncode(String strSrc)
  6. htmlEncode(String text)
  7. htmlEncode(String txt)
  8. htmlEncode(String txt)
  9. htmlEncoded(String text)