code: public static String nameEncode(String value) {
  
          if (value == null)
              return null;
  
          // make buffer roomy
          StringBuilder encodedValue = new StringBuilder(value.length() * 2);
  
          int length = value.length();
          int last = length - 1;
  
          for (int i = 0; i < length; i++) {
  
              char c = value.charAt(i);
  
              // space first or last
              if (c == ' ' && (i == 0 || i == last)) {
                  encodedValue.append("\\ ");
                  continue;
              }
  
              if (c < NAME_ESCAPE_TABLE.length) {
                  // check in table for escapes
                  String esc = NAME_ESCAPE_TABLE[c];
  
                  if (esc != null) {
                      encodedValue.append(esc);
                      continue;
                  }
              }
  
              // default: add the char
              encodedValue.append(c);
          }
  
          return encodedValue.toString();
  
      }
docs: LDAP Encodes a value for use with a DN. Escapes for LDAP, not JNDI!
  
  <br>Escapes:<br> ' ' [space] - "\ " [if first or last] <br> '#'
  [hash] - "\#" <br> ',' [comma] - "\," <br> ';' [semicolon] - "\;" <br> '=
  [equals] - "\=" <br> '+' [plus] - "\+" <br> '&lt;' [less than] -
  "\&lt;" <br> '&gt;' [greater than] - "\&gt;" <br> '"' [double quote] -
  "\"" <br> '\' [backslash] - "\\" <br>
  
  @param value
  the value to escape.
  @return The escaped value.