code: public static String escapeWithBackslash(String value, String metachars) {
          StringBuffer sb = new StringBuffer();
          int i, n = value.length();
          char ch;
          for (i=0; i<n; i++) {
              ch = value.charAt(i);
              if (ch=='\\' || metachars.indexOf(ch)>=0) {
                  sb.append('\\');
              }
              sb.append(ch);
          }
          return sb.toString();
      }
docs: Escape meta characters with backslash.
  
  @param value original string
  @param metachars characters that need to be escaped
  @return modified string with meta characters escaped with backslashes