lv.ctco.cukesrest.internal.helpers.Strings Maven / Gradle / Ivy
The newest version!
package lv.ctco.cukesrest.internal.helpers;
public class Strings {
public static boolean containsIgnoreCase(CharSequence str, CharSequence searchStr) {
if(str != null && searchStr != null) {
int len = searchStr.length();
int max = str.length() - len;
for(int i = 0; i <= max; ++i) {
if(regionMatches(str, true, i, searchStr, 0, len)) {
return true;
}
}
return false;
} else {
return false;
}
}
static boolean regionMatches(CharSequence cs, boolean ignoreCase, int thisStart, CharSequence substring, int start, int length) {
if(cs instanceof String && substring instanceof String) {
return ((String)cs).regionMatches(ignoreCase, thisStart, (String)substring, start, length);
} else {
int index1 = thisStart;
int index2 = start;
int tmpLen = length;
while(tmpLen-- > 0) {
char c1 = cs.charAt(index1++);
char c2 = substring.charAt(index2++);
if(c1 != c2) {
if(!ignoreCase) {
return false;
}
if(Character.toUpperCase(c1) != Character.toUpperCase(c2) && Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
return false;
}
}
}
return true;
}
}
public static boolean isEmpty(CharSequence cs) {
return cs == null || cs.length() == 0;
}
public static String escapeRegex(String regex) {
return regex.replace("{", "\\{").replace("}", "\\}");
}
}