Java Utililty Methods Unicode

List of utility methods to do Unicode

Description

The list of methods to do Unicode are organized into topic(s).

Method

intunicodeCount(String sStr)
unicode Count
if (sStr == null || sStr.equals("")) {
    return 0;
int count = 0;
for (int i = 0; i < sStr.length(); i++) {
    if ((int) sStr.charAt(i) > 255) {
        count++;
return count;
StringunicodeEncode(String s)
unicode encoding (for verbose mode)
StringBuilder buf = new StringBuilder();
for (int i = 0; i < s.length(); ++i) {
    char ch = s.charAt(i);
    if (ch >= '\u0080') {
        String st = Integer.toHexString(0x10000 + (int) ch);
        while (st.length() < 4)
            st = "0" + st;
        buf.append("\\u").append(st.subSequence(1, 5));
...
StringunicodeHTMLEscape(final String s)
Perform Unicode Escape on Specified String.
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    char[] hexChars = new char[4];
    if ((c >> 7) > 0) {
        sb.append("&#"); 
        hexChars[0] = hexChar[(c >> 12) & 0xF]; 
        hexChars[1] = hexChar[(c >> 8) & 0xF]; 
...
intunicodePreservingIndex(String str, int index)
Normalizes index such that it respects Unicode character boundaries in str .
if (index > 0 && index < str.length()) {
    if (Character.isHighSurrogate(str.charAt(index - 1)) && Character.isLowSurrogate(str.charAt(index))) {
        return index - 1;
return index;
StringunicodePreservingSubstring(String str, int begin, int end)
Returns a substring of str that respects Unicode character boundaries.
return str.substring(unicodePreservingIndex(str, begin), unicodePreservingIndex(str, end));
StringunicodeToChar(char[] unicode)
unicode To Char
return String.valueOf((char) Integer.parseInt(String.valueOf(unicode), 16));
StringunicodeToHTMLUnicodeEntity(final String text)
unicode To HTML Unicode Entity
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) {
...
StringunicodeTrim(String s)
unicode Trim
final int length = s.length();
if (length == 0)
    return s;
int start = 0;
while (start < length) {
    char c = s.charAt(start);
    if (c == ' ' || c == '\n') {
        start += 1;
...