String 方法 equalsIgnoreCase
学习方法
知其所以然 知其所以然,才能知其然
首先直接来看string
你可以尝试一下这样写法
String string = new String("1");
直奔主题 使用idea 在main方法中书写上面代码
复制源代码以及上面的注释到你想要写的地方 源类 或者你的编辑文件
在类中编辑请加上注释 // 或者 /** */
之后 观察源码 开始动手
/** * Compares this {@code String} to another {@code String}, ignoring case * considerations. Two strings are considered equal ignoring case if they * are of the same length and corresponding characters in the two strings * are equal ignoring case. * *Two characters {@code c1} and {@code c2} are considered the same * ignoring case if at least one of the following is true: *
-
*
- The two characters are the same (as compared by the * {@code ==} operator) *
- Applying the method {@link * java.lang.Character#toUpperCase(char)} to each character * produces the same result *
- Applying the method {@link * java.lang.Character#toLowerCase(char)} to each character * produces the same result *
备注一下这些都是什么意思 也就是@后面的
@ param
@ param标签可以归档方法或构造器的某个单一参数,或者归档类、接口以及泛型方法的类型参数。在使用@ param标签时,我们应该针对方法的每一个参数都使用一个该标签。每个段落的第一个词会被当作参数名,而余下的部分则会被当作是对它的描述:
@see
@ see标签可以创建链接到其他javadoc文档的交叉引用。我们可以在该标签的后面命名任何标识符,尽管我们必须对它们进行充分的限定。
看源码
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 equalsIgnoreCase(String anotherString){ return (this == anotherString) ? true: (anotherString != null) && (anotherString.value.length == value.length) && regionMatches(true, 0, anotherString, 0, value.length); }
知识点:三元运算符 然后短路与 参数 就不用讲了
上面注释的 翻译
//将此{@code String}与另一个{@code String}进行比较,忽略大小写考虑。
// 如果两个字符串长度相同,且两个字符串中的相应字符大小写相等,则视为相等忽略大小写<p>
// 两个字符{@code c1}和{@code c2}被视为相同的忽略大小写,如果以下至少一个为真:<ul><li>这两个字符相同
// (通过{@code=}运算符进行比较)<li>对每个字符应用方法{@link java.lang.CharactertoUpperCase(char)}
// 会产生相同的结果<li>应用该方法{@link java.lang.CharactertoLowerCase(char)}
// 到每个字符都会产生相同的结果<ul>@param anotherString{@code String}
// 如果参数不是{@code null}并且它表示等效的{@code String}忽略大小写,
// 则将此{@code String}与@return{@code true}进行比较{@code false}
// 否则@see equals(Object)
/*
分析方法:
public 范围修饰符
boolean 方法返回值
equalsIgnoreCase 忽略大小写比较
equalsIgnoreCase() 方法用于将字符串与指定的对象比较,不考虑大小写
this 指向当前对象
value.length 值的长度
length属性是用来说明数组的长度;
2.length使用来说明字符串String的长度
带() 是方法 不带是属性
具体分析:
return (this == anotherString) ? true : (anotherString != null)
// 值是否相等 对象内存是否相等
&& (anotherString.value.length == value.length)
// 比较传入值得参数的长度
&& regionMatches(true, 0, anotherString, 0, value.length);
// regionMatches() 方法用于检测两个字符串在一个区域内是否相等。 // regionMatches 区域匹配如果字符串的指定子区域匹配字符串参数的指定子区域,则返回 true;否则返回 false。 是否完全匹配或考虑大小写取决于 ignoreCase 参数。
分析 regionMatches() 方法 查看参数
参数
boolean ignoreCase, ignoreCase -- 如果为 true,则比较字符时忽略大小写。
int toffset, toffset -- 此字符串中子区域的起始偏移量。
String other, other -- 字符串参数。
int ooffset, ooffset -- 字符串参数中子区域的起始偏移量。
int len len -- 要比较的字符数。
方法内主要知识点:
>>> (符号) num>>>n 逻辑右移,当num为正数和算术右移一个效果 java中的移位运算符:<<,>>,>>>
//<< : 左移运算符,num << 1,相当于num乘以2
//
//>> : 右移运算符,num >> 1,相当于num除以2
//
//>>> : 无符号右移,忽略符号位,空位都以0补齐
//
// // Note: toffset, ooffset, or len might be near -1>>>1.
// Java8的lambda表达式。就是匿名函数。
// while (len-- > 0) {
// char c1 = ta[to++];
// char c2 = pa[po++];
// if (c1 == c2) {
// continue;
// }
匿名函数格式
父类名|接口名 对象名 = new 父类构造器(参数列表)|实现接口()
{
//匿名内部类的类体部分
}
匿名内部类必须要继承一个父类或者实现一个接口,
当然也仅能只继承一个父类或者实现一个接口。
同时它也是没有class关键字,
这是因为匿名内部类是直接使用new来生成一个对象的引用。当然这个引用是隐式的。
</pre>
#### 课外知识 移位运算符 实例
```java
public static void main(String[] args) {
int number = 10;
//原始数二进制
printInfo(number);
number = number << 1;
//左移一位
printInfo(number);
number = number >> 1;
//右移一位
printInfo(number);
}
private static void printInfo(int num) {
System.out.println(Integer.toBinaryString(num));
}
学会看从基本看方法,一步一步解决.