Java字符串equals()

Java String equals() method is used to compare this string with the passed object as argument.

Java字符串equals()方法用于将该字符串与传递的对象作为参数进行比较。

Java字符串equals() (Java String equals())

  • Java String equals() method overrides the Object class equals() method implementation.

    Java字符串equals()方法将覆盖Object类的equals()方法实现。
  • Since String is immutable, checking the equality of string to another object should be done using equals() method rather than == operator.

    由于String是不可变的 ,因此应使用equals()方法而不是==运算符来检查字符串与另一个对象的equals()
  • String equals() method always return boolean value, it doesn’t throw any exceptions.

    字符串equals()方法始终返回布尔值,它不会引发任何异常。
  • The result of equals() method is true if and only if – the argument is not null, it’s a String object, represents same sequence of characters as this string.

    当且仅当–参数不为null,它是一个String对象,表示与此字符串相同的字符序列时,equals()方法的结果为true
  • Below is the code snippet showing implementation details of equals() method.
    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String aString = (String)anObject;
            if (coder() == aString.coder()) {
                return isLatin1() ? StringLatin1.equals(value, aString.value)
                                  : StringUTF16.equals(value, aString.value);
            }
        }
        return false;
    }

    The method uses some of the internal classes and functions of String class, just have a look how it’s written in properly optimized way.

    该方法使用String类的一些内部类和函数,只是看看它是如何以适当优化的方式编写的。

  • If you want case insensitive equality check, then you can use String equalsIgnoreCase() method. It’s signature is public boolean equalsIgnoreCase(String anotherString), note that here the argument is String object.

    如果要进行不区分大小写的相等性检查,则可以使用String equalsIgnoreCase()方法。 它的签名是public boolean equalsIgnoreCase(String anotherString) ,请注意,这里的参数是String对象。

Java字符串equals()方法示例 (Java String equals() method example)

Here is a short example of string equals() method.

这是字符串equals()方法的简短示例。

package com.journaldev.string;

public class JavaStringEqualsExample {

	public static void main(String[] args) {
		String str1 = "abc";
		String str2 = "abc";

		boolean isEqual = str1.equals(str2);

		System.out.println(isEqual); //true
	}

}

Java字符串equalsIgnoreCase()方法示例 (Java String equalsIgnoreCase() method example)

Here is a short code snippet showing how to use equalsIgnoreCase() method.

这是显示如何使用equalsIgnoreCase()方法的简短代码段。

String s1 = "ABC";
String s2 = "abc";
String s3 = "abcd";
System.out.println(s1.equalsIgnoreCase(s2)); //true
System.out.println(s1.equalsIgnoreCase(s3)); //false

用户输入的Java字符串equals()示例 (Java String equals() example with user input)

Here is an example where two strings are compared using equals() and equalsIgnoreCase() methods. Both of these string are entered by user on command prompt and we are using Scanner class to read them.

这是使用equals()和equalsIgnoreCase()方法比较两个字符串的示例。 这两个字符串都是由用户在命令提示符下输入的,我们正在使用Scanner类读取它们。

package com.journaldev.string;

import java.util.Scanner;

public class JavaStringEqualsExample {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter First String:");
		String str1 = sc.nextLine();
		System.out.println("Enter Second String:");
		String str2 = sc.nextLine();
		sc.close();

		System.out.println("Both Strings are Equal? = " + str1.equals(str2));
		System.out.println("Both Strings are Case Insensitive Equal? = " + str1.equalsIgnoreCase(str2));

	}

}

Here is a sample output produced when above program is executed in Eclipse IDE.

这是在Eclipse IDE中执行上述程序时产生的输出示例。

That’s all for Java String equals() method usage and examples.

Java字符串equals()方法的用法和示例就到此为止。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/18302/java-string-equals

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值