Here you can find the source of quote(String value)
public static String quote(String value)
//package com.java2s; /*// w w w . j a v a 2 s . co m * Copyright 2010 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ public class Main { /** * Safely escape an arbitrary string as a JSON string literal. */ public static String quote(String value) { StringBuilder toReturn = new StringBuilder("\""); for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); String toAppend = String.valueOf(c); switch (c) { case '\b': toAppend = "\\b"; break; case '\t': toAppend = "\\t"; break; case '\n': toAppend = "\\n"; break; case '\f': toAppend = "\\f"; break; case '\r': toAppend = "\\r"; break; case '"': toAppend = "\\\""; break; case '\\': toAppend = "\\\\"; break; default: if (isControlChar(c)) { toAppend = escapeCharAsUnicode(c); } } toReturn.append(toAppend); } toReturn.append("\""); return toReturn.toString(); } private static boolean isControlChar(char c) { return (c >= 0x00 && c <= 0x1f) || (c >= 0x7f && c <= 0x9f) || c == '\u00ad' || c == '\u070f' || c == '\u17b4' || c == '\u17b5' || c == '\ufeff' || (c >= '\u0600' && c <= '\u0604') || (c >= '\u200c' && c <= '\u200f') || (c >= '\u2028' && c <= '\u202f') || (c >= '\u2060' && c <= '\u206f') || (c >= '\ufff0' && c <= '\uffff'); } /** * Turn a single unicode character into a 32-bit unicode hex literal. */ private static String escapeCharAsUnicode(char toEscape) { String hexValue = Integer.toString(toEscape, 16); int padding = 4 - hexValue.length(); return "\\u" + ("0000".substring(0, padding)) + hexValue; } }