字符串的比较
- (1)==:比较对象的地址
- 结论:只有两个字符串的常量对象比较时才会返回true,其他的都是false
@Test
public void test01(){
String s1 = "hello";
String s2 = "hello";
System.out.println(s1 == s2);
}
@Test
public void test02(){
String s1 = new String("hello");
String s2 = "hello";
System.out.println(s1 == s2);
}
@Test
public void test03(){
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);
}
- (2)equals:比较字符串的内容,严格区分大小写
- 因为String类型重写了Object的equals
@Test
public void test04(){
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1.equals(s2));
String s3 = "hello";
System.out.println(s3.equals(s1));
String s4 = "Hello";
System.out.println(s3.equals(s4));
}
- (3)equalsIgnoreCase(String anotherString) :比较字符串内容,不区分大小写
@Test
public void test05(){
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1.equalsIgnoreCase(s2));
String s3 = "hello";
System.out.println(s3.equalsIgnoreCase(s1));
String s4 = "Hello";
System.out.println(s3.equalsIgnoreCase(s4));
}
- (4)大小比较
- String类型实现了Comparable接口,重写了compareTo方法,严格区分大小写
- 依次比较对应位置的字符
- hello和Hello,先[0]位置的h和H,h>H,就直接认定为hello>Hello
- hello和hella,先[0][1][2][3]比较,都一样,最后到[4]o>a,认定hello>hella
- hello和helloworld,发现前面都一样,长的大
@Test
public void test06(){
String s1 = new String("hello");
String s2 = new String("helloworld");
if(s1.compareTo(s2) > 0){
System.out.println(s1 + ">" + s2);
}else if(s1.compareTo(s2) < 0){
System.out.println(s1 + "<" + s2);
}else{
System.out.println(s1 + "=" + s2);
}
}
- (5)大小比较:不区分大小写
- String类型提供了一个方法compareToIgnoreCase,可以忽略大小写比较大小
@Test
public void test07(){
String s1 = new String("hello");
String s2 = new String("Hello");
if(s1.compareToIgnoreCase(s2) > 0){
System.out.println(s1 + ">" + s2);
}else if(s1.compareToIgnoreCase(s2) < 0){
System.out.println(s1 + "<" + s2);
}else{
System.out.println(s1 + "=" + s2);
}
}
- (6)按照每个国家的语言校对顺序
- java.text.Collator:Collator 类执行区分语言环境的 String 比较。使用此类可为自然语言文本构建搜索和排序例程。
-
Collator实现了Comparator接口 ,
-
Collator是抽象类,不能直接创建对象,它有一个直接子类RuleBasedCollator
-
Collator内部提供了一个静态方法,可以获取一个它的子类对象
- 自然排序:实现java.lang.Comparable接口,int compareTo(Object obj)
- 定制排序
@Test
public void test08(){
String[] arr = {"hello","chai","Java","Alice","Hi"};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
@Test
public void test09(){
String[] arr = {"hello","chai","Java","Alice","Hi"};
Arrays.sort(arr,new Comparator(){
@Override
public int compare(Object o1, Object o2) {
String s1 = (String) o1;
String s2 = (String) o2;
return s1.compareToIgnoreCase(s2);
}
});
System.out.println(Arrays.toString(arr));
}
@Test
public void test10(){
String[] arr = {"李四","张三","王五","赵六","孙七"};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
@Test
public void test11(){
String[] arr = {"李四","张三","王五","赵六","孙七"};
Arrays.sort(arr, Collator.getInstance());
System.out.println(Arrays.toString(arr));
}
@Test
public void test12(){
String[] arr = {"李四","张三","王五","赵六","孙七"};
Arrays.sort(arr, Collator.getInstance(Locale.CHINA));
System.out.println(Arrays.toString(arr));
}