版权声明:欢迎关注博主公众号:矿洞程序员 https://blog.csdn.net/qq_32423845/article/details/89336144
源码如下:
核心思路:取到每一个字符,比较如果全部相等则返回true
代码语言:javascript复制public boolean equals(Object anObject){
if(this==anObject){
return true;
}
if(anObject instanceof String){
String anotherString=(String)anObject;
int n=value.length;
if(n==anObject.value.length){
char v1[]=value;
char v2[]=anotherString.value;
int i=0;
while(n--!=0){
if(v1[i]!=v2[i])
return false;
i ;
}
return true;
}
}
return false;
}
代码语言:javascript复制public boolean equalsIgnoreCase(String anotherString){
return (this==anotherString)?true:(anotherString!=null)&&(anotherString.value.length==value.length)&&
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=offset;
//Note:toOffset ,offset,or len might be near -1>>1
if((ooffset<0) || (toOffset<0)|| (toffset>(long)value.length-len) || (offset>(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;
}
核心思路:字符比较。如果字符转为大写或者小写。相等 则相等