我是靠谱客的博主 健壮口红,这篇文章主要介绍解析String中忽略大小写方法的原理,现在分享给大家,希望可以做个参考。

1. 解析String中忽略大小写方法的原理

方法初探

复制代码
1
2
3
4
5
6
7
public boolean equalsIgnoreCase(String anotherString) { return (this == anotherString) ? true : (anotherString != null) && (anotherString.value.length == value.length) && regionMatches(true, 0, anotherString, 0, value.length); }

可以看出 此方法 核心是 regionMatches方法 只要 他能运行 并且返回为true 此方法就返回为true

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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; if ((ooffset < 0) || (toffset < 0) || (toffset > (long)value.length - len) || (ooffset > (long)other.value.length - len)) { return false; } while (len-- > 0) { char c1 = ta[to++]; char c2 = pa[po++]; if (c1 == c2) { continue; } if (ignoreCase) { char u1 = Character.toUpperCase(c1); char u2 = Character.toUpperCase(c2); if (u1 == u2) { continue; } if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) { continue; } } return false; } return true; }

假如比较 字符串 s1,s2 s1.equalsIgnoreCase(s2); 会来到 regionMatches方法中

regionMatches有五个参数:

  • ignoreCase – 如果为 true,则比较字符时忽略大小写。
  • toffset – s1字符串的起始位置。
  • other – 字符串参数。
  • ooffset – s2的哪个位置开始比较。
  • len – 要比较的字符数。

regionMatches(true, 0, anotherString, 0, value.length)方法的参数列表 表示 是要比较大小写并且都从起始位置开始比较

总结:如果字符串的指定子区域匹配字符串参数的指定子区域,则返回 true;否则返回 false。是否完全匹配或考虑大小写取决于 ignoreCase 参数。

最后

以上就是健壮口红最近收集整理的关于解析String中忽略大小写方法的原理的全部内容,更多相关解析String中忽略大小写方法内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(86)

评论列表共有 0 条评论

立即
投稿
返回
顶部