Java Utililty Methods HTML Escape

List of utility methods to do HTML Escape

Description

The list of methods to do HTML Escape are organized into topic(s).

Method

StringhtmlEscape(String S)
html Escape
if (null == S) {
    return S;
int N = S.length();
StringBuilder sb = new StringBuilder(N);
for (int i = 0; i < N; i++) {
    char c = S.charAt(i);
    if (c == '&') {
...
StringhtmlEscape(String s)
Turn special characters into HTML character references.
if (s == null) {
    return null;
StringBuffer escaped = new StringBuffer(s.length());
for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    if ((c >= 0 && c <= 33) || (c >= 35 && c <= 37) || (c >= 39 && c <= 59) || (c == 61)
            || (c >= 63 && c <= 159)) {
...
Stringhtmlescape(String s)
Turns funky characters into HTML entity equivalents

e.g.

if (s == null)
    return "";
StringBuffer buf = new StringBuffer();
int n = s.length();
for (int i = 0; i < n; ++i) {
    char ch = s.charAt(i);
    String entity = (String) i2e.get(Integer.valueOf(ch));
    if (entity == null) {
...
StringhtmlEscape(String source)
Turns funky characters into HTML entity equivalents.

E.g.

StringBuffer buf = new StringBuffer();
if (source != null) {
    for (int i = 0; i < source.length(); ++i) {
        char ch = source.charAt(i);
        String entity = (String) i2e.get(new Integer((int) ch));
        if (entity == null) {
            if (((int) ch) > 128)
                buf.append("&#" + ((int) ch) + ";");
...
StringhtmlEscape(String str)
html Escape
StringBuilder esc = new StringBuilder();
for (int i = 0; i < str.length(); ++i) {
    final char c = str.charAt(i);
    switch (c) {
    case '<':
        esc.append("&lt;");
        break;
    case '>':
...
StringhtmlEscape(String str)
Escape ampersands and less-than characters in a string using HTML-style &amp; and &lt; constructs.
String rval = "";
String s = str;
while (true) {
    int ix1 = s.indexOf((int) '&');
    int ix2 = s.indexOf((int) '<');
    if (ix1 < 0 && ix2 < 0) {
        break;
    int ix;
    if (ix1 < 0) {
        ix = ix2;
    } else if (ix2 < 0) {
        ix = ix1;
    } else {
        ix = ix1 < ix2 ? ix1 : ix2;
    rval += s.substring(0, ix);
    s = s.substring(ix);
    if (s.startsWith("&")) {
        rval += "&amp;";
    } else {
        rval += "&lt;";
    s = s.substring(1);
rval += s;
return rval;
StringhtmlEscape(String str)
html Escape
return str.replace(">", "&gt;").replace("<", "&lt;").replace("&", "&amp;");
StringhtmlEscape(String string)
html Escape
StringBuilder builder = new StringBuilder();
for (int i = 0; i < string.length(); i++) {
    char nonEscaped = string.charAt(i);
    String escaped = charToHtmlEntity(nonEscaped);
    if (escaped == null)
        builder.append(nonEscaped);
    else
        builder.append(escaped);
...
StringhtmlEscape(String tag)
Replace characters having special meaning inside HTML tags with their escaped equivalents, using character entities such as '&'.
final StringBuffer result = new StringBuffer();
if (tag == null) {
    return null;
} else {
    for (int i = 0; i < tag.length(); i++) {
        char character = tag.charAt(i);
        if (character == '<') {
            result.append("&lt;");
...
StringhtmlEscape(String text)
Performs necessary escaping to render arbitrary plain text as plain text without any markup.
StringBuilder buf = new StringBuilder(text.length() + 64);
for (int i = 0; i < text.length(); i++) {
    char ch = text.charAt(i);
    if (ch == '<')
        buf.append("&lt;");
    else if (ch == '&')
        buf.append("&amp;");
    else
...