compare To() 按字典顺序比较字符串,返回int值;
equals() 比较两个字符串的内容是否相等
equalsIgnoreCase() 忽略大小写,比较两个字符串的内容是否相等 ,返回boolean值;
eg1:Sting类下compare To()方法:
</pre><pre name="code" class="java">public class compareTo
{
public static void main(String[] args)
{
String str="abcdefg";
String str1="abckl9o";
String st2r="abcdefg";
System.out.println(str.compareTo(str1));
System.out.println(str.compareTo(str2));
System.out.println(str1.compareTo(str));
}
}
运行结果:-7
0
7
</pre><p>eg2:对象的equals()方法:</p><p></p><pre name="code" class="java">
public class equals{
public static void main(){
String str= new String("wang");
String str1="wang";
String str2=new String("wang");
String str3="wa"+"ng";
String s="ng";
String str4="wa"+s;
System.out.println(str.equals(str1));
System.out.println(str.equals(str2));
System.out.println(str1.equals(str2));
System.out.println(str1.equals(str3));
System.out.println(str1.equals(str4));
}
}
运行结果:true
true
true
true
eg3:equalsIgnoreCase()方法
public class equalsIgnoreCase
{
public static void main(String[] args)
{
String str="abcde";
String str1="abcde";
String st2r="ABCDE";
System.out.println(str1.compareTo(str3));
System.out.println(str1.equals(str2));
System.out.println(str1.<span style="font-size: 18.01801872253418px; font-family: Arial, Helvetica, sans-serif;">equals</span><span style="font-size: 18.01801872253418px; font-family: Arial, Helvetica, sans-serif;">(str3));</span>
<span style="white-space:pre"> </span>System.out.println(str1.equalsIgnoreCase(str3));
}
}