Java Unicode Escape unicodeEscaped(final String str)

Here you can find the source of unicodeEscaped(final String str)

Description

Returns the Subl escaped string representing the input string Each character that is not 7 bit ASCII and ASCII control characters are escaped in the following form where xxxx is the hex representation of the character (it may be from 2 to 6 hex digits as needed).

License

LGPL

Parameter

Parameter Description
str The string to be converted.

Return

the unicode escaped string.

Declaration

public static String unicodeEscaped(final String str) 

Method Source Code

//package com.java2s;
/**/*from  ww w.jav  a  2  s .co m*/
 * Provides <tt>String</tt> utilities not otherwise provided by Violin Strings.  All methods
 * are static.  There is no need to instantiate this class.<p>
 *
 * @version $Id: StringUtils.java 127764 2009-05-05 17:13:00Z tbrussea $
 * @author Stephen L. Reed
 *
 * <p>Copyright 2001 Cycorp, Inc., license is open source GNU LGPL.
 * <p><a href="http://www.opencyc.org/license.txt">the license</a>
 * <p><a href="http://www.opencyc.org">www.opencyc.org</a>
 * <p><a href="http://www.sourceforge.net/projects/opencyc">OpenCyc at SourceForge</a>
 * <p>
 * THIS SOFTWARE AND KNOWLEDGE BASE CONTENT ARE PROVIDED ``AS IS'' AND
 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENCYC
 * ORGANIZATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE AND KNOWLEDGE
 * BASE CONTENT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

public class Main {
    /** Returns the Subl escaped string representing the input string
     *  Each character that is not 7 bit ASCII and ASCII control characters are escaped
     *  in the following form where xxxx is the hex representation of the character
     *  (it may be from 2 to 6 hex digits as needed). The escaped representation is then &uxxxx;
     *  The semicolon at the end is required. Also '&' is escaped to &u26;
     * @param str The string to be converted.
     * @return the unicode escaped string.
     */
    public static String unicodeEscaped(final String str) {
        //System.out.println("UnicodeEscaped Entry: |"+str+"|");
        String estr = escapeDoubleQuotes(str);
        //System.out.println("UnicodeEscaped esc  |"+estr+"|");
        // now we have to change "true unicode chars( value>=0x80 ) to sublisp escaped values
        StringBuilder sb = new StringBuilder(str.length());
        char c = 0; // note: change to int after we switch to java 1.5
        int i;
        for (i = 0; i < estr.length(); i++) {
            c = estr.charAt(i);// note: change to codePointAt after we switch to java 1.5
            if (c >= 0x20 && c < 0x80) {
                if (c == '&')
                    sb.append("&u26;");
                else
                    sb.append(c);
            } else {
                sb.append("&u");
                String hex = Integer.toHexString((int) c);
                if (hex.length() < 2)
                    sb.append('0');
                sb.append(hex);
                sb.append(';');
            }

        }
        //System.out.println("UnicodeEscaped exit |"+sb.toString()+"|");
        return "(list #$UnicodeStringFn \"" + sb.toString() + "\")";
    }

    /**
     * Escapes embedded double quote and backslash characters in the given string.
     *
     * @param string the given string
     * @return the given string with embeded double quote characters
     * preceded by a backslash character, and with embedded backslash
     * characters preceded by another (escaping) backslash character
     */
    public static String escapeDoubleQuotes(String string) {
        if (string == null) {
            return null;
        }
        String result = string.replaceAll("\\\\", "\\\\\\\\");
        return result.replaceAll("\\\"", "\\\\\\\"");
    }
}

Related

  1. unicodeEscape(StringBuilder result, int in, int index)
  2. unicodeEscaped(char ch)
  3. unicodeEscaped(Character ch)
  4. unicodeEscaped(Character ch)
  5. unicodeEscaped(final Character ch)
  6. unicodeEscapeEncode(String unicode)
  7. unicodeHexEscapeJava(char ch)
  8. unicodeUnescape(String st)