dap4/d4core/src/main/java/dap4/core/util/Escape.java
static public String
    backslashEscape(String s, String wrt)
    {
        if(wrt == null)
            wrt = BACKSLASHESCAPE;
        StringBuilder escaped = new StringBuilder();
        for(int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if(c < ' ' || c == 127) {
                escaped.append('\\');
                switch (c) {
                case '\r':
                    c = 'r';
                    break;
                case '\n':
                    c = 'n';
                    break;
                case '\t':
                    c = 't';
                    break;
                case '\f':
                    c = 'f';
                    break;
                default:
                    escaped.append('x');
                    escaped.append(Escape.toHex((int) c));
                    continue; /* since this is a string */
                }
            } else if(c == '\\' || wrt.indexOf(c) >= 0)
                escaped.append('\\');
            escaped.append(c);
        }
        return escaped.toString();
    }
static public String
    backslashEscape(String s, String wrt)
    {
        if(wrt == null)
            wrt = BACKSLASHESCAPE;
        StringBuilder escaped = new StringBuilder();
        for(int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if(c < ' ' || c == 127) {
                escaped.append('\\');
                switch (c) {
                case '\r':
                    c = 'r';
                    break;
                case '\n':
                    c = 'n';
                    break;
                case '\t':
                    c = 't';
                    break;
                case '\f':
                    c = 'f';
                    break;
                default:
                    escaped.append('x');
                    escaped.append(Escape.toHex((int) c));
                    continue; /* since this is a string */
                }
            } else if(c == '\\' || wrt.indexOf(c) >= 0)
                escaped.append('\\');
            escaped.append(c);
        }
        return escaped.toString();
    }
Escape control chars plus
selected other characters in a string using backslash
The definitive list is in netcdf-c/ncgen/ncgen.l.

@param s   the string to escape
@param wrt what printable characters to escape;
control characters and '\\' are always escaped.
@return