Java String EqualsignoreCase示例

时间:2020-02-23 14:34:48  来源:igfitidea点击:

String的EqualSignoreCase方法类似于等于方法,但它忽略了case。
因此,如果字符串有不同的情况,那么它也将被认为是等于的。

例如:

String str1="theitroad.com"
String str2="theitroad.COM"

str1.equalsignorecase(str2)将返回true。

方法签名:

public boolean equalsIgnoreCase(Object anObject)

字符串EqualSignoreCase示例:

package org.arpit.theitroad;
 
public class StringEqualsExamplesMain {
 public static void main(String[] args) {
  String str1 = "Hello world from theitroad.com";
  String str2 = new String("Hello world from theitroad.com");
  String str3 = "Hello world from theitroad.COM";
 
  if (str1.equalsIgnoreCase(str2)) {
   System.out.println("1st and 2nd Strings are equal");
  } 
  else {
   System.out.println("1st and 2nd Strings are not equal");
  }
 
  if (str1.equalsIgnoreCase(str3)) {
   System.out.println("1st and 3rd Strings are equal");
  } 
  else {
   System.out.println("1st and 3rd Strings are not equal");
  }
 }
}

运行上面的程序时,我们将得到以下输出:

1st and 2nd Strings are equal
1st and 3rd Strings are equal