cdm/src/main/java/ucar/unidata/util/StringUtil2.java
static public String escape(String x, String okChars) {
    StringBuilder newname = new StringBuilder();
    for (char c : x.toCharArray()) {
      if (c == '%') {
        newname.append("%%");
      } else if (!Character.isLetterOrDigit(c) && okChars.indexOf(c) < 0) {
        newname.append('%');
        newname.append(Integer.toHexString((0xFF & (int) c)));
      } else
        newname.append(c);
    }
    return newname.toString();
  }
static public String escape(String x, String okChars) {
    StringBuilder newname = new StringBuilder();
    for (char c : x.toCharArray()) {
      if (c == '%') {
        newname.append("%%");
      } else if (!Character.isLetterOrDigit(c) && okChars.indexOf(c) < 0) {
        newname.append('%');
        newname.append(Integer.toHexString((0xFF & (int) c)));
      } else
        newname.append(c);
    }
    return newname.toString();
  }
static public String escape ( String x , String okChars ) { StringBuilder newname = new StringBuilder ( ) ; for ( char c : x . toCharArray ( ) ) { if ( c == ' ' ) { newname . append ( "%%" ) ; } else if ( ! Character . isLetterOrDigit ( c ) && okChars . indexOf ( c ) < 0 ) { newname . append ( ' ' ) ; newname . append ( Integer . toHexString ( ( 0xFF & ( int ) c ) ) ) ; } else newname . append ( c ) ; } return newname . toString ( ) ; }
Escape any char not alphanumeric or in okChars.
Escape by replacing char with %xx (hex).

@param x       escape this string
@param okChars these are ok.
@return equivilent escaped string.
Escape any char not alphanumeric or in okChars . Escape by replacing char with %xx ( hex ) .