短視訊帶貨原始碼,對於輸入的驗證碼,不區分大小寫
阿新 • • 發佈:2022-04-13
短視訊帶貨原始碼,對於輸入的驗證碼,不區分大小寫
適用於驗證碼校驗(不區分大小寫)
public class TestString {
public static void main(String[] args) {
testEquals("asd", "asd");
testEquals("asD", "Asd");
}
/**
* @see java.lang.String#equalsIgnoreCase
* usage: checking vertification code
*/
private static void testEquals(String a, String b) {
System.out.println("\"" + a + "\".equals(\"" + b + "\") = " + a.equals(b));
System.out.println("\"" + a + "\".equalsIgnoreCase(\"" + b + "\") = " + a.equalsIgnoreCase(b));
}
}
class TestString {
companion object {
@JvmStatic
fun main(args: Array<String>) {
testEquals("asd", "asd")
testEquals("asD", "Asd")
}
private fun testEquals(a: String, b: String) {
println("\"$a\".equals(\"$b\") = " + (a == b))
println("\"$a\".equalsIgnoreCase(\"$b\") = " + a.equals(b, ignoreCase = true))
}
}
}
執行結果
"asd".equals("asd") = true
"asd".equalsIgnoreCase("asd") = true
"asD".equals("Asd") = false
"asD".equalsIgnoreCase("Asd") = true
原始碼
public boolean equalsIgnoreCase(String anotherString) {
return (this == anotherString) ? true
: (anotherString != null)
&& (anotherString.value.length == value.length)
&& regionMatches(true, 0, anotherString, 0, value.length);
}
1、優先判斷記憶體地址值是否一致:this == anotherString
2、不為null且長度一致的情況下: regionMatches(true, 0, anotherString, 0, value.length)
public boolean regionMatches(boolean ignoreCase, int toffset,
String other, int ooffset, int len) {
char ta[] = value;
int to = toffset;
char pa[] = other.value;
int po = ooffset;
// Note: toffset, ooffset, or len might be near -1>>>1.
//1、判斷是否陣列越界
if ((ooffset < 0) || (toffset < 0)
|| (toffset > (long)value.length - len)
|| (ooffset > (long)other.value.length - len)) {
return false;
}
//2、迴圈遍歷char是否一致
while (len-- > 0) {
char c1 = ta[to++];
char c2 = pa[po++];
if (c1 == c2) {
continue;
}
if (ignoreCase) {//是否忽略大小寫,true 則同義轉換為大寫或同義轉換為小寫進行校驗
// If characters don't match but case may be ignored,
// try converting both characters to uppercase.
// If the results match, then the comparison scan should
// continue.
char u1 = Character.toUpperCase(c1);
char u2 = Character.toUpperCase(c2);
if (u1 == u2) {
continue;
}
// Unfortunately, conversion to uppercase does not work properly
// for the Georgian alphabet, which has strange rules about case
// conversion. So we need to make one last check before
// exiting.
if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
continue;
}
}
return false;
}
return true;
}
以上就是 短視訊帶貨原始碼,對於輸入的驗證碼,不區分大小寫,更多內容歡迎關注之後的文章