Home  >  Article  >  Java  >  How to compare strings in java

How to compare strings in java

(*-*)浩
(*-*)浩Original
2019-11-12 10:17:379438browse

How to compare strings in java

Java provides us with compareTo, "==", and equals to compare strings. Let's introduce their differences.

Example 1: compareTo compare the size of data  (Recommended learning: java course)

compareTo(string)
compareToIgnoreCase(String)
compareTo(object string)

This example is done by using The above function compares two strings and returns an int type. If the string is equal to the parameter string, 0 is returned; if the string is less than the parameter string, the return value is less than 0; if the string is greater than the parameter string, the return value is greater than 0.

The basis for judging the size of strings is based on their order in the dictionary.

package com.de.test;

/**
 * Java字符串比较大小
 */
public class StringA {
    public static void main(String[] args){
        String str = "String";
        String anotherStr = "string";
        Object objstr = str;
        System.out.println(str.compareTo(anotherStr));
        System.out.println(str.compareToIgnoreCase(anotherStr));
        System.out.println(str.compareTo(objstr.toString()));
    }
}

Execute the above code to produce the following results

-32
0
0

Example 2: Use equals(), "==" method to compare strings

Use equals() and ==. The difference is that equals compares whether the contents are equal, and == compares whether the referenced variable addresses are equal.

package com.de.test;

public class StringA {
    public static void main(String[] args){
        String s1 = "hello";
        String s2 = "hello";
        String s3 = new String("hello");
        String s4 = new String("hello");
        System.out.println("s1:" + s1);
        System.out.println("s2:" + s2);
        System.out.println("s3:" + s3);
        System.out.println("s4:" + s4);
        System.out.println("----------比较内容是否相等---------------");
        System.out.println(s1.equals(s2));
        System.out.println(s2.equals(s3));
        System.out.println(s3.equals(s4));
        System.out.println("----------比较引用地址是否相等---------------");
        System.out.println(s1 == s2);
        System.out.println(s2 == s3);
        System.out.println(s3 == s4);
    }
}

Executing the above code produces the following results

s1:hello
s2:hello
s3:hello
s4:hello
----------比较内容是否相等---------------
true
true
true
----------比较引用地址是否相等---------------
true
false
false

The above is the detailed content of How to compare strings in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what is java apiNext article:what is java api