Here you can find the source of quoteJavaString(String s)
Parameter | Description |
---|---|
s | the text to convert |
public static String quoteJavaString(String s)
//package com.java2s; //License from project: Open Source License public class Main { /**/*w w w.ja v a2 s .co m*/ * Convert a string to the Java literal and enclose it with double quotes. * Null will result in "null" (without double quotes). * * @param s the text to convert * @return the Java representation */ public static String quoteJavaString(String s) { if (s == null) { return "null"; } return "\"" + javaEncode(s) + "\""; } /** * Convert a string to a Java literal using the correct escape sequences. * The literal is not enclosed in double quotes. The result can be used in * properties files or in Java source code. * * @param s the text to convert * @return the Java representation */ public static String javaEncode(String s) { int length = s.length(); StringBuilder buff = new StringBuilder(length); for (int i = 0; i < length; i++) { char c = s.charAt(i); switch (c) { // case '\b': // // BS backspace // // not supported in properties files // buff.append("\\b"); // break; case '\t': // HT horizontal tab buff.append("\\t"); break; case '\n': // LF linefeed buff.append("\\n"); break; case '\f': // FF form feed buff.append("\\f"); break; case '\r': // CR carriage return buff.append("\\r"); break; case '"': // double quote buff.append("\\\""); break; case '\\': // backslash buff.append("\\\\"); break; default: int ch = c & 0xffff; if (ch >= ' ' && (ch < 0x80)) { buff.append(c); // not supported in properties files // } else if(ch < 0xff) { // buff.append("\\"); // // make sure it's three characters (0x200 is octal 1000) // buff.append(Integer.toOctalString(0x200 | ch).substring(1)); } else { buff.append("\\u"); String hex = Integer.toHexString(ch); // make sure it's four characters for (int len = hex.length(); len < 4; len++) { buff.append('0'); } buff.append(hex); } } } return buff.toString(); } }