Java Unicode Create toUnicode(String text)

Here you can find the source of toUnicode(String text)

Description

to Unicode

License

Open Source License

Declaration

public static String toUnicode(String text) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {

    public static String toUnicode(String text) {
        int bufLen = text.length() * 2;
        StringBuilder sb = new StringBuilder(bufLen);
        for (int i = 0; i < text.length(); i++) {
            char chr = (char) text.charAt(i);

            if ((chr > 61) && (chr < 127)) {
                if (chr == '\\') {
                    sb.append('\\');
                    sb.append('\\');
                    continue;
                }// w  w w  .jav a  2 s  . c o  m
                sb.append(chr);
                continue;
            }

            if (isChinese(chr))
                sb.append("\\u" + Integer.toHexString(text.charAt(i) & 0xffff));
            else
                sb.append(chr);
        }
        return sb.toString();
    }

    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }
}

Related

  1. toUnicode(String str)
  2. toUnicode(String str)
  3. toUnicode(String str)
  4. toUnicode(String string)
  5. toUnicode(String strText)
  6. toUnicode(String text)
  7. toUnicode(String theString, boolean escapeSpace)
  8. toUnicode(String value)
  9. toUnicode(StringBuilder strBuilder, char ch)