Java HTML Encode htmlEncode(String string)

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

Description

Escapes all characters that may cause a problem if a string is used directly in HTML.

License

Open Source License

Parameter

Parameter Description
string the string to encode.

Return

the string with all single and double quotes, ampersands, greater than and less than symbols, pound symbols, semicolons and non-ASCII characters transformed into their equivalent HTML numerical entities.

Declaration

public static String htmlEncode(String string) 

Method Source Code

//package com.java2s;
/*/*from  ww  w. j  av a2s  .  c  o  m*/
 * TextUtilities.java (Class: com.madphysicist.tools.util.TextUtilities)
 *
 * Mad Physicist JTools Project (General Purpose Utilities)
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2012 by Joseph Fox-Rabinovitz
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

public class Main {
    /**
     * Escapes all characters that may cause a problem if a string is used
     * directly in HTML. Characters are replaced by their equivalent numerical
     * entities. For example, ampersand ({@code &}) becomes {@code &}
     * rather than {@code &}. The escaped characters are single quote
     * ({@code '}), double quote ({@code "}), ampersand ({@code &}), less than
     * ({@code <}), greater than ({@code >}), pound ({@code #}), semicolon
     * ({@code ;}), and any character whose code is greater than 127.
     *
     * @param string the string to encode.
     * @return the string with all single and double quotes, ampersands, greater
     * than and less than symbols, pound symbols, semicolons and non-ASCII
     * characters transformed into their equivalent HTML numerical entities.
     */
    public static String htmlEncode(String string) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            if (c > 127 || c == '\"' || c == '\'' || c == '&' || c == '<' || c == '>' || c == '#' || c == ';')
                sb.append("&#").append((int) c).append(';');
            else
                sb.append(c);
        }
        return sb.toString();
    }
}

Related

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