Java HTML Encode toHtml(String s)

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

Description

Returns the specified string converted to a format suitable for HTML.

License

Open Source License

Parameter

Parameter Description
s the string to convert

Return

the converted string

Declaration

public static String toHtml(String s) 

Method Source Code

//package com.java2s;
/*/*from  ww  w.j  a  v  a  2s  .c om*/
 This file is part of Subsonic.

 Subsonic is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Subsonic is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Subsonic.  If not, see <http://www.gnu.org/licenses/>.

 Copyright 2009 (C) Sindre Mehus
 */

public class Main {
    private static final String[][] HTML_SUBSTITUTIONS = {
            { "&", "&amp;" }, { "<", "&lt;" }, { ">", "&gt;" },
            { "'", "&#39;" }, { "\"", "&#34;" }, };

    /**
     * Returns the specified string converted to a format suitable for
     * HTML. All single-quote, double-quote, greater-than, less-than and
     * ampersand characters are replaces with their corresponding HTML
     * Character Entity code.
     *
     * @param s the string to convert
     * @return the converted string
     */
    public static String toHtml(String s) {
        if (s == null) {
            return null;
        }
        for (String[] substitution : HTML_SUBSTITUTIONS) {
            if (s.contains(substitution[0])) {
                s = s.replaceAll(substitution[0], substitution[1]);
            }
        }
        return s;
    }
}

Related

  1. toHTML(String org, boolean inputValue)
  2. toHtml(String p_str)
  3. toHTML(String plainText)
  4. toHTML(String rawText)
  5. toHTML(String s)
  6. toHtml(String str)
  7. toHtml(String str)
  8. toHtml(String str)
  9. toHtml(String str)