Java Unicode Escape unicodeEscape(String s)

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

Description

unicode Escape

License

BSD License

Declaration

public static String unicodeEscape(String s) 

Method Source Code

//package com.java2s;
//License from project: BSD License 

public class Main {
    public static String unicodeEscape(String s) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            sb.append(unicodeEscape(c));
        }/*from w  w  w  .  j  a v  a 2s . c  om*/
        return sb.toString();
    }

    /**
     * Converts the string to the unicode format '\u0020'.
     * 
     * This format is the Java source code format.
     *
     * <pre>
     *   UnicodeUtils.unicodeEscape(' ') = "\u0020"
     *   UnicodeUtils.unicodeEscaped('A') = "\u0041"
     * </pre>
     * 
     * @param ch  the character to convert
     * @return the escaped unicode string
     */
    public static String unicodeEscape(char ch) {
        if (ch < 0x10) {
            return "\\u000" + Integer.toHexString(ch);
        } else if (ch < 0x100) {
            return "\\u00" + Integer.toHexString(ch);
        } else if (ch < 0x1000) {
            return "\\u0" + Integer.toHexString(ch);
        }
        return "\\u" + Integer.toHexString(ch);
    }
}

Related

  1. unicodeEscape(char c)
  2. unicodeEscape(Character ch)
  3. unicodeEscape(String s)
  4. unicodeEscape(StringBuilder result, int in, int index)
  5. unicodeEscaped(char ch)
  6. unicodeEscaped(Character ch)