Java HTML Unescape unescapeHtmlByNumber(final String input)

Here you can find the source of unescapeHtmlByNumber(final String input)

Description

Unescapes HTML characters based on ASCII numbers.

License

Apache License

Parameter

Parameter Description
input the input

Declaration

private static String unescapeHtmlByNumber(final String input) 

Method Source Code

//package com.java2s;
/*/*from  www  .ja  v a2s.c om*/
 * Copyright 2014 Roque Pinel
 *
 * Licensed 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.
 */

public class Main {
    /**
     * Unescapes HTML characters based on ASCII numbers.
     * 
     * @param input the input
     * @return
     */
    private static String unescapeHtmlByNumber(final String input) {
        if (input.indexOf("&#") == -1)
            return input;

        String out = "";

        for (int i = 0, length = input.length(); i < length; i++) {
            if (input.charAt(i) == '&' && i + 1 < length && input.charAt(i + 1) == '#') {
                int j = i + 2;
                boolean isValid = true;
                int n = 0;
                for (; j < length; j++) {
                    char c = input.charAt(j);
                    if (c == ';')
                        break;
                    else if (c >= '0' || c <= '9') {
                        n = n * 10 + (c - '0');
                    } else {
                        isValid = false;
                        break;
                    }
                }
                if (isValid && n != 0) {
                    char unescape = (char) n;
                    out += unescape;
                    i = j;
                } else {
                    out += input.charAt(i);
                }
            } else {
                out += input.charAt(i);
            }
        }

        return out;
    }
}

Related

  1. unEscapeHtml(String text)
  2. unescapeHTML(String value)
  3. unescapeHTML(String value)
  4. unescapeHTML2(String source, int start)
  5. unescapeHtmlByName(final String input)
  6. unEscapeHTMLTag(String input)