Java String Java Encode javaEncode(String txt)

Here you can find the source of javaEncode(String txt)

Description

java Encode

License

Apache License

Declaration

public static String javaEncode(String txt) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {

    public static String javaEncode(String txt) {
        if ((txt == null) || (txt.length() == 0)) {
            return txt;
        }//www .  j  a v a 2  s . c om
        txt = replace(txt, "\\", "\\\\");
        txt = replace(txt, "\r\n", "\n");
        txt = replace(txt, "\r", "\\r");
        txt = replace(txt, "\t", "\\t");
        txt = replace(txt, "\n", "\\n");
        txt = replace(txt, "\"", "\\\"");
        txt = replace(txt, "'", "\\'");
        return txt;
    }

    public static String replace(String str, String subStr, String reStr) {
        if (str == null) {
            return null;
        }
        if ((subStr == null) || (subStr.equals("")) || (reStr == null)) {
            return str;
        }
        return str.replace(subStr, reStr);
    }
}

Related

  1. javaEncode(String s)
  2. javaEncode(String s)
  3. javaEncode(String txt)