Java String to convertString2GSonString(String str)

Here you can find the source of convertString2GSonString(String str)

Description

convert String G Son String

License

Open Source License

Declaration

public static String convertString2GSonString(String str) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015 Frank Becker and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from   www.ja v a  2s.  c  o  m*/
 *     Frank Becker - initial API and implementation
 *******************************************************************************/

public class Main {
    public static String convertString2GSonString(String str) {
        str = str.replace("\"", "\\\"").replace("\n", "\\\n"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$//$NON-NLS-4$
        StringBuffer ostr = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if ((ch >= 0x0020) && (ch <= 0x007e)) {
                ostr.append(ch);
            } else {
                ostr.append("\\u"); //$NON-NLS-1$
                String hex = Integer.toHexString(str.charAt(i) & 0xFFFF);
                for (int j = 0; j < 4 - hex.length(); j++) {
                    ostr.append("0"); //$NON-NLS-1$
                }
                ostr.append(hex.toLowerCase());
            }
        }
        return (new String(ostr));
    }
}

Related

  1. convertString(String s)
  2. convertString(String s, Class cls)
  3. convertString(String strValue)
  4. convertString(String value, Class type)
  5. convertString2Bytes(String str)
  6. convertStringArrayToFloatArray(String[] num)
  7. convertStringArrayToString(String[] a_asStrings, String a_sDelim)
  8. convertStringArrayToString(String[] value, String separator)
  9. convertStringFormat(String property)