Skip to content

Instantly share code, notes, and snippets.

@imandaliya
Created December 21, 2022 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imandaliya/4fc17e0abb83893529dcbbf32da5954b to your computer and use it in GitHub Desktop.
Save imandaliya/4fc17e0abb83893529dcbbf32da5954b to your computer and use it in GitHub Desktop.
// compare two strings on java with neglating localization
public static boolean containsIgnoreCase(String src, String what) {
final int length = what.length();
if (length == 0)
return true; // Empty string is contained
final char firstLo = Character.toLowerCase(what.charAt(0));
final char firstUp = Character.toUpperCase(what.charAt(0));
for (int i = src.length() - length; i >= 0; i--) {
// Quick check before calling the more expensive regionMatches() method:
final char ch = src.charAt(i);
if (ch != firstLo && ch != firstUp)
continue;
if (src.regionMatches(true, i, what, 0, length))
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment