比较Java中的两个字符串–equals(),compareTo()方法
时间:2020-01-09 10:34:53 来源:igfitidea点击:
如果我们必须比较Java中的两个String或者基于这些String的内容比较两个String的一部分,则可以使用以下方法之一进行比较:
- equals()方法或者equalsIgnoreCase()(如果我们不想考虑大小写)。参见示例。
- compareTo()方法或者compareToIgnoreCase()(如果我们不想考虑大小写)。参见示例。
- 为了比较String的一部分,可以使用startsWith()和endsWith()方法。参见示例。
- 要将一个String的区域与另一个String的指定区域进行比较,可以使用regionMatches()方法。参见示例。
使用equals()和equalsIgnoreCase()方法比较字符串
- boolean equals(Object anObject)–将此字符串与指定对象进行比较。如果传递的参数不为null,并且是具有与此String相同的字符序列的String对象,则此方法返回true。
- boolean equalsIgnoreCase(String anotherString)–将此字符串与另一个String进行比较,而忽略大小写考虑。如果两个字符串的长度相同并且两个字符串中的对应字符相等,则认为它们相等(忽略大小写)。
使用equals()方法的Java示例
public class StringComapre {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
String str3 = "hello";
//returns true
System.out.println("str1.equals(str2)-" + str1.equals(str2));
//returns false as case is different
System.out.println("str1.equals(str3)-" + str1.equals(str3));
}
}
输出:
str1.equals(str2)-true str1.equals(str3)-false
使用equalsIgnoreCase()方法的Java示例
public class StringComapre {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
String str3 = "hello";
//returns true
System.out.println("str1.equals(str2)-" + str1.equals(str2));
//returns true as case is ignored
System.out.println("str1.equalsIgnoreCase(str3)-" + str1.equalsIgnoreCase(str3));
}
}
输出:
str1.equals(str2)-true str1.equalsIgnoreCase(str3)-true
使用compareTo()和compareToIgnoreCase()方法比较字符串
- int compareTo(String anotherString)–按字典顺序比较两个字符串。如果此String大于参数,则返回正整数;如果此String小于参数,则返回负整数;如果此字符串等于参数,则返回零。
- int compareToIgnoreCase(String str)–按字典顺序比较两个字符串,忽略大小写差异。如果此String大于参数,则返回正整数;如果此String小于参数,则返回负整数;如果此字符串与参数相等,则返回零(忽略大小写考虑)。
使用compareTo()方法的Java示例
public class StringComapre {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
String str3 = "Halo";
// returns 0
System.out.println("str1.compareTo(str2): " + str1.compareTo(str2));
// returns positive integer
System.out.println("str1.compareTo(str3): " + str1.compareTo(str3));
// returns negative integer
System.out.println("str3.compareTo(str1): " + str3.compareTo(str1));
}
}
输出:
str1.compareTo(str2): 0 str1.compareTo(str3): 4 str3.compareTo(str1): -4
由于str1和str2具有相同的值,因此比较它们时返回0。在将str1与str3比较时,返回正整数(4),因为" hello"之后是" hello"。
使用compareToIgnoreCase()方法的Java示例
public class StringComapre {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "hello";
String str3 = "cello";
// returns 0
System.out.println("str1.compareTo(str2): " + str1.compareToIgnoreCase(str2));
// returns positive integer
System.out.println("str1.compareTo(str3): " + str1.compareTo(str3));
// returns negative integer
System.out.println("str3.compareTo(str1): " + str3.compareTo(str1));
}
}
输出:
str1.compareTo(str2): 0 str1.compareTo(str3): -27 str3.compareTo(str1): 27
使用startsWith()和endsWith()方法比较字符串部分
- boolean startsWith(String str)–测试此字符串是否以传递的参数开头。如果子字符串开头匹配,则返回true,否则返回false。
- boolean startsWith(String str,int toffset)–测试此字符串的子字符串是否从指定索引处开始,并以传递的参数开头。如果子字符串开头匹配,则返回true,否则返回false。
- boolean EndsWith(String str)–测试此字符串是否以传递的参数结尾。如果子字符串末尾匹配,则返回true,否则返回false。
Java示例使用startsWith()和endsWith()
public class StringComapre {
public static void main(String[] args) {
String str = "Compare this String";
// returns true
System.out.println("str.startsWith(\"Compare\"): " + str.startsWith("Compare"));
// returns false
System.out.println("str.startsWith(\"Comparison\"): " + str.startsWith("Comparison"));
// returns true- Comparison starts from index 8
System.out.println("str.startsWith(\"this\"): " + str.startsWith("this", 8));
// returns true
System.out.println("str.endsWith(\"String\"): " + str.endsWith("String"));
// returns false
System.out.println("str.endsWith(\"Sting\"): " + str.endsWith("Sting"));
}
}
输出:
str.startsWith("Compare"): true
str.startsWith("Comparison"): false
str.startsWith("this"): true
str.endsWith("String"): true
str.endsWith("Sting"): false
使用regionMatches方法比较字符串部分
- boolean regionMatches(int toffset,String other,int ooffset,int len)–测试两个字符串区域是否相等。将第一个字符串的子字符串与第二个字符串的子字符串进行比较。使用toffset指定第一个字符串的子字符串从其开始的索引。使用ooffset指定第二个字符串的子字符串从其开始的索引。使用len指定要比较的子字符串的长度。
- boolean regionMatches(boolean ignoreCase,int toffset,String other,int ooffset,int len)–如果将ignoreCase传递为true,则在比较字符时忽略大小写。
public class StringComapre {
public static void main(String[] args) {
String str1 = "Compare this string";
String str2 = "Compare with this String";
// Comparing "this" portion of both Strings- true
System.out.println("str1.regionMatches(8, str2, 13, 4): " + str1.regionMatches(8, str2, 13, 4));
// Comparing "String" portion of both Strings- false when case is considered
System.out.println("str1.regionMatches(13, str2, 18, 6): " + str1.regionMatches(13, str2, 18, 6));
// Comparing "String" portion of both Strings- true when case is ignored
System.out.println("str1.regionMatches(true, 13, str2, 18, 6): " + str1.regionMatches(true, 13, str2, 18, 6));
}
}
输出:
str1.regionMatches(8, str2, 13, 4): true str1.regionMatches(13, str2, 18, 6): false str1.regionMatches(true, 13, str2, 18, 6): true

