短视频带货源码,对于输入的验证码,不区分大小写
适用于验证码校验(不区分大小写)
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; }
以上就是 短视频带货源码,对于输入的验证码,不区分大小写,更多内容欢迎关注之后的文章