Java 比较String对象的方法

Java中有许多比较String的方法,弄清楚每个方法的使用方式与效果对我们编写代码有益。


public class TestString {
    private String myself="abc";
    public int compare1(String s){
        return myself.compareTo(s);
    }
    public int compare2(String s ){
        return myself.compareToIgnoreCase(s);
    }
    public boolean compare3(CharSequence s){
        return myself.contentEquals(s);
    }
    public boolean compare4(String s){
        return myself.equals(s);
    }
    public boolean compare5(String s){
        return myself.equalsIgnoreCase(s);
    }
    public boolean compare6(String s){
        return (myself==s);
    }
    public static void main(String args[]){
        TestString test=new TestString();
        System.out.println("Method 1:");
        String s1="abc";
        String s11="DDc";
        System.out.println("Mehtod compareTo (\""+test.myself+"\", \""+s1+"\"): "+test.compare1(s1));
        System.out.println("Mehtod compareTo (\""+test.myself+"\", \""+s11+"\"): "+test.compare1(s11));
        System.out.println("Method 2:");
        String s2="ABC";
        System.out.println("Mehtod compareToIgnoreCase (\""+test.myself+"\", \""+s2+"\"): "+test.compare2(s2));
        System.out.println("Method 3:");
        CharSequence cSequence1="abc";
        CharSequence cSequence2="def";
        System.out.println("Mehtod contentEquals (\""+test.myself+"\", \""+cSequence1+"\"): "+test.compare3(cSequence1));
        System.out.println("Mehtod contentEquals (\""+test.myself+"\", \""+cSequence2+"\"): "+test.compare3(cSequence2));
        System.out.println("Method 4:");
        String s4="abc";
        String s5=s4;
        System.out.println("Mehtod equals (\""+test.myself+"\", \""+s4+"\"): "+test.compare4(s4));
        System.out.println("Mehtod equals (\""+s4+"\", \""+s5+"\"): "+s4.equals(s5));
        System.out.println("Method 5:");
        String s6="abC";
        System.out.println("Mehtod equalsIgnoreCase (\""+test.myself+"\", \""+s6+"\"): "+test.compare5(s6));
        System.out.println("Method 6:");
        String s7="abc";
        String s8=s7;
        String s9=new String("abc");
        System.out.println("Mehtod Operator (\"=\" \""+test.myself+"\", \""+s7+"\"): "+test.compare6(s7));
        System.out.println("Mehtod Operator (\"=\" \"s7\", \"s8\"): "+(s7==s8)+" "+test.compare6(s8));
        System.out.println("Mehtod Operator (\"=\" \"s7\", \"s9\"): "+(s7==s9));
        System.out.println("Test  \"==\" in basic type");
        int a1=7;
        int a2=7;
        System.out.println("(a1==a2): "+(a1==a2));
        System.out.println("Test equals and  \"==\" in type Integer");
        Integer a3=new Integer(7);
        Integer a4=new Integer(7);
        System.out.println("(a3==a4): "+(a3==a4));
        System.out.println("a3.equals(a4): "+a3.equals(a4));
    }

}
/*
output:
Method 1:
Mehtod compareTo ("abc", "abc"): 0
Mehtod compareTo ("abc", "DDc"): 29
Method 2:
Mehtod compareToIgnoreCase ("abc", "ABC"): 0
Method 3:
Mehtod contentEquals ("abc", "abc"): true
Mehtod contentEquals ("abc", "def"): false
Method 4:
Mehtod equals ("abc", "abc"): true
Mehtod equals ("s4", "s5"): true
Method 5:
Mehtod equalsIgnoreCase ("abc", "abC"): true
Method 6:
Mehtod Operator"=" ("abc", "abc"): true
Mehtod Operator"=" ("s7", "s8"): true true
Mehtod Operator"=" ("s7", "s9"): false
Test  "==" in basic type
(a1==a2): true
Test equals and  "==" in type Integer
(a3==a4): false
a3.equals(a4): true
*/

说明:
1. int compareTo(String s):该方法比较两个String对象的内容是否相同。返回值分别为正数,零,负数。该方法将String对象中每一个字符转换成Unicode进行比较,若两个String对象内容相同,则返回零;若给定参数Unicode大于被比较对象,则返回正数,且值为第一个不相同字符的Unicode差;否则返回负数。
2. int compareToIgnoreCase(String s):该方法比较两个String对象内容是否相同,并且忽略大小写。
3. boolean contentEquals(CharSequence cSequence):该方法顾名思义,比较两个String对象内容是否相同。相同返回True,不同返回False。
4. boolean equals(String s): 该方法比较两个String对象内容是否相同。相同返回True,不同返回False。
5. boolean equalsIgnoreCase(String s):该方法与equals()一样,只是忽略了大小写。
6. “==”:使用该符号进行比较,比较的是两者的内存地址。test.myself,s7,s8指向同一个字符串常量,因此结果为True。而s9是新创建的对象,所以结果为false。

equals与“==”:
对于基本数据类型(byte,short,char,int,long,float,double,boolean),”==”比较的是值;对于非基本数据类型,”==”比较的是内存中的存放地址。基本数据类型没有equals方法,也可以从侧面说明数据类型并没有继承Object。
equals方法是Object类中定义的,用于比较内存中的存放地址。因为在Object中该方法的实现就是用”==”实现的。但在一些类中该方法已经被覆盖,String类就覆盖了这个方法,改为比较对象的内容。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值