Java Unicode unicodeCodepointToString(char cp, boolean shortenIfPossible, String prefix, boolean upperCase)

Here you can find the source of unicodeCodepointToString(char cp, boolean shortenIfPossible, String prefix, boolean upperCase)

Description

Like #unicodeCodepointToString(char,boolean) if you pass in "\\u" as prefix.

License

Open Source License

Declaration

public static String unicodeCodepointToString(char cp, boolean shortenIfPossible, String prefix,
        boolean upperCase) 

Method Source Code

//package com.java2s;
/*//from  w w w  . j  a v  a  2  s .co  m
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 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. Unicode2ASCII(String unicode)
  2. unicode2Byte(String s)
  3. Unicode2GBK(String dataStr)
  4. unicode2native(String s)
  5. unicodeChar(char a, char b, char c, char d)
  6. unicodeConvert(String str)
  7. unicodeCount(String sStr)
  8. unicodeEncode(String s)
  9. unicodeHTMLEscape(final String s)