Java HTML Encode htmlEntityEncode(String s)

Here you can find the source of htmlEntityEncode(String s)

Description

Encodes given string with utf-8 encoding for html.

License

Open Source License

Parameter

Parameter Description
s a parameter

Declaration

public static String htmlEntityEncode(String s) 

Method Source Code

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

public class Main {
    /**/*from  w w  w .j  av  a2 s. com*/
     * Encodes given string with utf-8 encoding for html.
     * 
     * @param s
     * @return
     */
    public static String htmlEntityEncode(String s) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9') {
                buf.append(c);
            } else {
                buf.append("&#" + (int) c + ";");
                /*if (c == '/' || c == '<' || c == '>' || c == ' ' || c == '\r'
                  || c == '\n' || c == '"') {
                   buf.append(c);
                } else {
                   buf.append("&#" + (int) c + ";");
                }*/
            }
        }
        return buf.toString();
    }
}

Related

  1. htmlEncode(String txt)
  2. htmlEncode(String txt)
  3. htmlEncoded(String text)
  4. htmlEncoder(String content)
  5. htmlEncoding(String str)
  6. htmlize(CharSequence q)
  7. htmlize(String str)
  8. htmlize(String text)
  9. htmlize(StringBuffer sb)