equals()
方法此方法将此字符串与指定对象进行比较。当且仅当参数不为null并且是一个String对象,表示与此对象相同的字符序列时,结果为true。
import java.lang.*; public class StringDemo { public static void main(String[] args) { String str1 = "sachin tendulkar"; String str2 = "amrood admin"; String str3 = "amrood admin"; //检查是否相等 boolean retval1 = str2.equals(str1); boolean retval2 = str2.equals(str3); //打印返回值 System.out.println("str2 is equal to str1 = " + retval1); System.out.println("str2 is equal to str3 = " + retval2); } }
str2 is equal to str1 = false str2 is equal to str3 = true
equalsIgnoreCase()
方法此方法与该ignoreCase()
方法相同,不同之处在于,如果两个字符串的长度相同,并且两个字符串中的对应字符相等,则忽略大小写,而忽略两个字符串。
import java.lang.*; public class StringDemo { public static void main(String[] args) { String str1 = "sachin tendulkar"; String str2 = "amrood admin"; String str3 = "AMROOD ADMIN"; //检查是否相等 with case ignored boolean retval1 = str2.equalsIgnoreCase(str1); boolean retval2 = str2.equalsIgnoreCase(str3); //打印返回值 System.out.println("str2 is equal to str1 = " + retval1); System.out.println("str2 is equal to str3 = " + retval2); } }
str2 is equal to str1 = false str2 is equal to str3 = true