Java String Code Point toCodeString(String s)

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

Description

Given a string, returns a string that could be printed out in Java source code.

License

Open Source License

Parameter

Parameter Description
s a parameter

Declaration

public static String toCodeString(String s) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from   www. ja  v a 2  s.  co m*/
     * Given a string, returns a string that could be printed out in Java source
     * code. That is, all escapable characters are reversed. The returned string
     * will already be surrounded by quotes.
     *
     * @param s
     * @return
     */
    public static String toCodeString(String s) {
        return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n").replace("\t", "\\t")
                + "\"";
    }
}

Related

  1. toCodePointArray(String string)
  2. toCodePoints(char[] src, int srcOff, int srcLen, int[] dest, int destOff)
  3. toCodepoints(String message)
  4. toCodePoints(String s)
  5. toCodePoints(String str)