Java Unicode to String unicodeStringToPrettyString(String s)

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

Description

Returns the most succinct possible, human-readable, ASCII form of the String s of Unicode codepoints.

License

Open Source License

Declaration

public static String unicodeStringToPrettyString(String s) 

Method Source Code

//package com.java2s;
/*/*from   w ww . ja  va2  s. c  om*/
The contents of this file are subject to the THDL Open Community License
Version 1.0 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License on the THDL web site 
(http://www.thdl.org/).
    
Software distributed under the License is distributed on an "AS IS" basis, 
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the 
License for the specific terms governing rights and limitations under the 
License. 
    
The Initial Developer of this software is the Tibetan and Himalayan Digital
Library (THDL). Portions created by the THDL are Copyright 2001 THDL.
All Rights Reserved. 
    
Contributor(s): ______________________________________.
*/

public class Main {
    /**
     * Returns the most succinct possible, human-readable, ASCII form
     * of the String s of Unicode codepoints. */
    public static String unicodeStringToPrettyString(String s) {
        if (s == null)
            return "null";
        StringBuffer sb = new StringBuffer(s.length() * 6);
        for (int i = 0; i < s.length(); i++) {
            sb.append(unicodeCodepointToString(s.charAt(i), true));
        }
        return sb.toString();
    }

    /** Returns a human-readable, ASCII form of the Unicode codepoint
    cp. If shortenIfPossible is true, then printable ASCII
    characters will appear as themselves. */
    public static String unicodeCodepointToString(char cp, boolean shortenIfPossible) {
        return unicodeCodepointToString(cp, shortenIfPossible, "\\u", false);
    }

    /** Like {@link #unicodeCodepointToString(char, boolean)} if you
    pass in <code>"\\u"</code> as prefix.  If you pass in the
    empty string as prefix, then U+0F55 will print as
    <code>0F55</code>. */
    public static String unicodeCodepointToString(char cp, boolean shortenIfPossible, String prefix,
            boolean upperCase) {
        if (shortenIfPossible) {
            if ((cp >= 'a' && cp <= 'z') || (cp >= 'A' && cp <= 'Z') || (cp >= '0' && cp <= '9') || cp == '\\'
                    || cp == '~' || cp == '`' || cp == '.' || cp == ',' || cp == ' ' || cp == '\'' || cp == '"'
                    || cp == '+' || cp == '-' || cp == '=' || cp == '_' || cp == '@' || cp == '!' || cp == '#'
                    || cp == '$' || cp == '%' || cp == '^' || cp == '&' || cp == '*' || cp == ':' || cp == '['
                    || cp == ']' || cp == '(' || cp == ')' || cp == '{' || cp == '}')
                return new String(new char[] { cp });
            if ('\t' == cp)
                return "\\t";
            if ('\n' == cp)
                return "\\n";
            if ('\r' == cp)
                return "\\r";
        }

        String suffix;
        if (cp < '\u0010')
            suffix = "000" + Integer.toHexString((int) cp);
        else if (cp < '\u0100')
            suffix = "00" + Integer.toHexString((int) cp);
        else if (cp < '\u1000')
            suffix = "0" + Integer.toHexString((int) cp);
        else
            suffix = Integer.toHexString((int) cp);
        return prefix + (upperCase ? suffix.toUpperCase() : suffix);
    }
}

Related

  1. Unicode2Str(String s)
  2. unicode2String(String unicodeStr)
  3. unicodeToAscii(String name, StringBuffer result)
  4. unicodeToAscii(String theString)
  5. unicodeToString(int unicode)
  6. unicodeToString(String escaped)