Skip to content

Instantly share code, notes, and snippets.

@aurbano
Last active August 29, 2015 13:57
Show Gist options
  • Save aurbano/9669278 to your computer and use it in GitHub Desktop.
Save aurbano/9669278 to your computer and use it in GitHub Desktop.
String case insensitive comparison. It has wildcard search implemented, so for example to search for nodes containing NAME, use *name*
/**
* String case insensitive comparison. It has wildcard search implemented, so
* for example to search for strings containing NAME somewhere, use *name*.
* Strings that start with name and end with .txt use name*.txt
* @param str1 String that you will match against.
* @param str2 String to search for.
* @return True if found
*/
private boolean compareStrings(final String str1, final String str2){
String[] split = str2.toLowerCase().split("\\*");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < split.length; i++) {
sb.append(Pattern.quote(split[i]));
if (i != split.length - 1) {
sb.append(".*");
}
}
String compareWith = sb.toString();
if(str2.substring(str2.length() - 1).equals("*")){
compareWith = compareWith+".*";
}
return str1.toLowerCase().matches(compareWith);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment