Java Unicode unicodeToHTMLUnicodeEntity(final String text)

Here you can find the source of unicodeToHTMLUnicodeEntity(final String text)

Description

unicode To HTML Unicode Entity

License

Open Source License

Declaration

public static String unicodeToHTMLUnicodeEntity(final String text) 

Method Source Code

//package com.java2s;
/*//from w w  w. j  ava 2s.com
 *  Freeplane - mind map editor
 *  Copyright (C) 2008 Joerg Mueller, Daniel Polansky, Christian Foltin, Dimitry Polivaev
 *
 *  This file is modified by Dimitry Polivaev in 2008.
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static String unicodeToHTMLUnicodeEntity(final String text) {
        /*
         * Heuristic reserve for expansion : factor 1.2
         */
        StringBuilder result = null;
        int intValue;
        char myChar;
        for (int i = 0; i < text.length(); ++i) {
            myChar = text.charAt(i);
            intValue = text.charAt(i);
            if (intValue < 32 || intValue > 126) {
                if (result == null) {
                    result = new StringBuilder((int) (text.length() * 1.2));
                    result.append(text.subSequence(0, i));
                }
                result.append("&#x").append(Integer.toString(intValue, 16)).append(';');
            } else if (result != null) {
                result.append(myChar);
            }
        }
        if (result != null)
            return result.toString();
        return text;
    }
}

Related

  1. unicodeEncode(String s)
  2. unicodeHTMLEscape(final String s)
  3. unicodePreservingIndex(String str, int index)
  4. unicodePreservingSubstring(String str, int begin, int end)
  5. unicodeToChar(char[] unicode)
  6. unicodeTrim(String s)