Java String Quote quoteJavaString(String s)

Here you can find the source of quoteJavaString(String s)

Description

Quotes a string literal for Java or JavaScript.

License

Open Source License

Parameter

Parameter Description
s Unquoted literal

Return

Quoted string literal

Declaration

public static String quoteJavaString(String s) 

Method Source Code

//package com.java2s;
/*/*  w w  w . jav a  2s. co m*/
 // This software is subject to the terms of the Eclipse Public License v1.0
 // Agreement, available at the following URL:
 // http://www.eclipse.org/legal/epl-v10.html.
 // You must accept the terms of that agreement to use this software.
 //
 // Copyright (C) 2001-2005 Julian Hyde
 // Copyright (C) 2005-2012 Pentaho and others
 // All Rights Reserved.
 */

public class Main {
    /**
     * Quotes a string literal for Java or JavaScript.
     *
     * @param s Unquoted literal
     * @return Quoted string literal
     */
    public static String quoteJavaString(String s) {
        return s == null ? "null" : "\""
                + s.replaceAll("\\\\", "\\\\\\\\").replaceAll("\\\"",
                        "\\\\\"") + "\"";
    }
}

Related

  1. quoteImpl(String s, char delim)
  2. quoteIt(String orig)
  3. quoteJava(String s)
  4. quoteJavascriptString(String s)
  5. quoteJavaString(String s)
  6. quoteJsonLib(String string)
  7. quoteLiteralAsRegexp(String text)
  8. quoteLocation(String location)
  9. quoteMdxIdentifier(String ident)