Java Char Convert To charToHTML(char ch, boolean xml)

Here you can find the source of charToHTML(char ch, boolean xml)

Description

Converts a character to HTML or XML entity.

License

Apache License

Parameter

Parameter Description
ch The character to convert.
xml Convert the character to XML if set to true.

Return

The converted string.

Declaration

public static final String charToHTML(char ch, boolean xml) 

Method Source Code

//package com.java2s;
/*/*from  w w  w. j  av  a  2  s.  com*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  The ASF licenses this file to You
 * under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.  For additional information regarding
 * copyright in this work, please see the NOTICE file in the top level
 * directory of this distribution.
 */

public class Main {
    /**
     * Converts a character to HTML or XML entity.
     *
     * @param ch The character to convert.
     * @param xml Convert the character to XML if set to true.
     * @author Erik C. Thauvin
     *
     * @return The converted string.
     */
    public static final String charToHTML(char ch, boolean xml) {
        int c;

        // Convert left bracket
        if (ch == '<') {
            return ("&lt;");
        }

        // Convert left bracket
        else if (ch == '>') {
            return ("&gt;");
        }

        // Convert ampersand
        else if (ch == '&') {
            return ("&amp;");
        }

        // Commented out to eliminate redundant numeric character codes (ROL-507)
        // High-ASCII character
        //else if (ch >= 128)
        //{
        //c = ch;
        //return ("&#" + c + ';');
        //}

        // Convert double quote
        else if (xml && (ch == '"')) {
            return ("&quot;");
        }

        // Convert single quote
        else if (xml && (ch == '\'')) {
            return ("&#39;");
        }

        // No conversion
        else {
            // Return character as string
            return (String.valueOf(ch));
        }
    }
}

Related

  1. charToCompressedBaseLow(final int base)
  2. charToCp1252(char c)
  3. charToDecimal(char c)
  4. charToDigit(char c)
  5. charToEscape(char ch)
  6. charToLong(final char value)
  7. charToLowerCase(char c)
  8. charToNCRef(int c)
  9. charToNum(char c)