Java String Escape escapeUnicode(String s)

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

Description

escape Unicode

License

Apache License

Declaration

public static String escapeUnicode(String s) 

Method Source Code

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

import java.util.Arrays;

public class Main {
    public static final char UNICODE_REPLACEMENT = 0xFFFD;

    public static String escapeUnicode(String s) {
        if (s == null) {
            return null;
        }//w  ww  . j  a  v a 2s.c om

        StringBuilder result = new StringBuilder();

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            char ca = (Character.isDefined(c)) ? c : UNICODE_REPLACEMENT;
            result.append(ca);
        }

        return result.toString();
    }

    public static String toString(Object object, Class<?> objectClass) {
        if (null == object) {
            return "null";
        }
        final String toString = object.toString();
        if (isStringEmpty(toString)) {
            return "\"\"";
        } else if (String.class.equals(objectClass)) {
            return "\"" + toString + '\"';
        } else {
            return toString;
        }
    }

    /**
     * Returns the string representation of the specified object, transparently
     * handling null references and arrays.
     * 
     * @param obj
     *            the object
     * @return the string representation
     */
    public static String toString(Object obj) {
        String result;
        if (obj != null) {
            if (obj instanceof boolean[]) {
                result = Arrays.toString((boolean[]) obj);
            } else if (obj instanceof byte[]) {
                result = Arrays.toString((byte[]) obj);
            } else if (obj instanceof char[]) {
                result = Arrays.toString((char[]) obj);
            } else if (obj instanceof double[]) {
                result = Arrays.toString((double[]) obj);
            } else if (obj instanceof float[]) {
                result = Arrays.toString((float[]) obj);
            } else if (obj instanceof int[]) {
                result = Arrays.toString((int[]) obj);
            } else if (obj instanceof long[]) {
                result = Arrays.toString((long[]) obj);
            } else if (obj instanceof Object[]) {
                result = Arrays.deepToString((Object[]) obj);
            } else if (obj instanceof short[]) {
                result = Arrays.toString((short[]) obj);
            } else {
                result = obj.toString();
            }
        } else {
            result = "null";
        }
        return result;
    }

    public static boolean isStringEmpty(String s) {
        return s == null || "".equals(s);
    }
}

Related

  1. escapeRegexpSymbol(String expr)
  2. escapeReservedWord(String input)
  3. escapeSelected(String str, String chars)
  4. escapeSolr(String value)
  5. escapeToFileName(String name)
  6. escapeUnicodeString(final String input, final boolean escapeAscii)
  7. escapeUnsafeCharacters(String anyURI, boolean escapePercent)
  8. escapeWiki(String s)
  9. escapeXML(String message)