code: public String formatForLiteral(String s) {
  
          if (s == null) {
              return "";
          }
  
          StringBuffer sb = new StringBuffer(s.length());
          char[] chars = s.toCharArray();
          for (char c : chars) {
              switch (c) {
                  case '\"':
                      sb.append("\\\"");
                      break;
                  case '\'':
                      sb.append("\\\'");
                      break;
                  case '\t':
                      sb.append("\\t");
                      break;
                  case '\r':
                      sb.append("\\r");
                      break;
                  case '\n':
                      sb.append("\\n");
                      break;
                  case '\f':
                      sb.append("\\f");
                      break;
                  case '\b':
                      sb.append("\\b");
                      break;
                  case '\\':
                      sb.append("\\\\");
                      break;
                  default:
                      sb.append(c);
                      break;
              }
          }
  
          return sb.toString();
      }
docs: Returns a String that is same as the specified String except that
  all special/escape characters are formatted as if they are escaped.
  <p>
  So, for example a tab character ('\t') becomes the string "\\t"
  
  @param s unescaped value
  @return escaped value