Skip to content

Instantly share code, notes, and snippets.

@yintro4ha
Created June 3, 2020 01:57
Show Gist options
  • Save yintro4ha/e3eb4ec2ed9affeb979ca02ff54e5d8a to your computer and use it in GitHub Desktop.
Save yintro4ha/e3eb4ec2ed9affeb979ca02ff54e5d8a to your computer and use it in GitHub Desktop.
public static final String escapeLDAPSearchFilter(String filter)
{
// If using JDK >= 1.5 consider using StringBuilder
StringBuffer sb = new StringBuffer();
for (int i = 0; i < filter.length(); i++) {
char curChar = filter.charAt(i);
switch (curChar) {
case '\\':
sb.append("\\5c");
break;
case '*':
sb.append("\\2a");
break;
case '(':
sb.append("\\28");
break;
case ')':
sb.append("\\29");
break;
case '\u0000':
sb.append("\\00");
break;
default:
sb.append(curChar);
}
}
return sb.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment