Here you can find the source of toJavascriptArray(String[][] Vals)
public static String toJavascriptArray(String[][] Vals)
//package com.java2s; /* =========================================================================== * Copyright (C) 2015 CapsicoHealth 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.//from w w w . j ava 2 s . c o m */ public class Main { public static String toJavascriptArray(String[][] Vals) { StringBuilder Str = new StringBuilder(); return toJavascriptArray(Str, Vals).toString(); } public static StringBuilder toJavascriptArray(StringBuilder Str, String[][] Vals) { boolean First = true; for (String[] v : Vals) { if (First == true) First = false; else Str.append(","); Str.append("["); toJavascriptArray(Str, v); Str.append("]"); } return Str; } public static String toJavascriptArray(String[] Vals) { StringBuilder Str = new StringBuilder(); return toJavascriptArray(Str, Vals).toString(); } public static StringBuilder toJavascriptArray(StringBuilder Str, String[] Vals) { boolean First = true; for (String v : Vals) { if (First == true) First = false; else Str.append(","); Str.append(EscapeJavaScriptQuotes(v)); } return Str; } public static final String EscapeJavaScriptQuotes(String S) { return EscapeHTMLJavaScriptQuotes(S, "'"); } public static final String EscapeHTMLJavaScriptQuotes(String S) { return EscapeHTMLJavaScriptQuotes(S, ""); } public static final String EscapeHTMLJavaScriptQuotes(String S, String BeforeAfter) { if (S == null) return BeforeAfter + BeforeAfter; StringBuilder X = new StringBuilder(128); X.append(BeforeAfter); int len = S.length(); char[] s = S.toCharArray(); int i = 0; int j = 0; while (j < len && s[j] != '"' && s[j] != '\'' && s[j] != '\n' && s[j] != '\r' && s[j] != '\\' && s[j] != '<') ++j; if (j >= len) { X.append(S); X.append(BeforeAfter); return X.toString(); } else do { char c = s[j]; X.append(s, i, j - i).append(c == '"' ? """ : c == '\'' ? "'" : c == '\n' ? "\\n" : c == '\\' ? "\\\\" : c == '<' ? "<" : ""); i = ++j; while (j < len && s[j] != '"' && s[j] != '\'' && s[j] != '\n' && s[j] != '\r' && s[j] != '\\' && s[j] != '<') ++j; } while (j < len); X.append(s, i, len - i); X.append(BeforeAfter); return X.toString(); } }