在 Java 中,字符序列称为字符串。它是不可变的(一旦创建就无法更改)并有助于执行多种操作。此外,字符串比较是 Java 中的常见编程任务。它可以通过多种方式执行,将在以下部分详细讨论。
正如已经讨论过的,可以使用不同的方法来完成字符串比较。他们是:
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
equals() 方法根据字符串内容比较两个字符串。如果字符串的大小写不同(即大写或小写),则将被视为不相等。下面是 equals() 方法的示例。
节目
public class StringComparisonExamples { public static void main(String[] args) { String str1 = "Balloon"; String str2 = "Balloon"; //string comparison using equals method if (str1.equals(str2)) { System.out.println("Both str1 : " + str1 + " and str2 : "+ str2 +" are equal"); } else { System.out.println("Both str1 : " + str1 + " and str2 : "+ str2 +" are not equal"); } String str3 = "Happy"; String str4 = "hai"; if (str3.equals(str4)) { System.out.println("Both str3 : " + str3 + " and str4 : "+ str4 +" are equal"); } else { System.out.println("Both str3 : " + str3 + " and str4 : "+ str4 +" are not equal"); } String str5 = "Happy"; String str6 = "hard"; if (str5.equals(str6)) { System.out.println("Both str5 : " + str5 + "and str6 : "+ str6 +" are equal"); } else { System.out.println("Both str5 : " + str5 + " and str6 : "+ str6 +" are not equal"); } } }
输出:
上述代码解释
在此方法中,按字典顺序比较值并返回整数类型的值。该值基于第一个字符串是否等于、小于或大于第 2 个nd 字符串。
存在两个字符串 str1 和 str2 并且
节目
//Java program to demonstrate compareTo method public class StringComparisonExamples { public static void main(String[] args) { String str1 = "Balloon"; String str2 = "Balloon"; String str3 = "Happy"; String str4 = "hai"; //string comparison using compareTo method System.out.println(str1.compareTo(str2)); System.out.println(str3.compareTo(str4)); String str5 = "Happy"; String str6 = "Hardest one"; System.out.println(str5.compareTo(str6)); } }
输出:
上述代码解释
使用此方法,将比较两个字符串,而不考虑字符串是大写还是小写。
节目
//Java program to demonstrate equalsIgnoreCase method public class StringComparisonExamples { public static void main(String[] args) { String str1 = "Balloon"; String str2 = "balloon"; //string comparison using equalsIgnoreCase method System.out.println(str1.equalsIgnoreCase(str2)); String str3 = "Happy"; String str4 = "hai"; System.out.println(str3.equalsIgnoreCase(str4)); String str5 = "Happy"; String str6 = "hard"; System.out.println(str5.equalsIgnoreCase(str6)); } }
输出:
上面代码的解释
该方法类似于compareTo方法,按字典顺序比较字符串。不同的是,无论字符串是大写还是小写,都不会影响比较。
节目
//Java program to demonstrate compareToIgnoreCase method public class StringComparisonExamples { public static void main(String[] args) { String str1 = "Balloon"; String str2 = "balloon"; //string comparison using compareToIgnoreCase method System.out.println(str1.compareToIgnoreCase(str2)); String str3 = "Happy"; String str4 = "hai"; System.out.println(str3.compareToIgnoreCase(str4)); String str5 = "Happy"; String str6 = "Hard"; System.out.println(str5.compareToIgnoreCase(str6)); } }
输出:
上面代码的解释
现在,让我们在一个程序中查看所有上述方法,以更好地理解字符串比较。
节目
//Java program to demonstrate different methods for string comparison public class StringComparisonExamples { public static void main(String[] args) { String str1 = "Balloon"; String str2 = "Balloon"; System.out.println("Comparison of str1 : " + str1 + " and str2 : " + str2); System.out.println("Using equals method "); //string comparison using equals method if (str1.equals(str2)) { System.out.println("Both are equal"); } else { System.out.println("Both are not equal"); } System.out.println("Using compareTo method "); //string comparison using compareTo method System.out.println(str1.compareTo(str2)); System.out.println("Using equalsIgnoreCase method "); //string comparison using equalsIgnoreCase method System.out.println(str1.equalsIgnoreCase(str2)); System.out.println("Using compareToIgnoreCase method "); //string comparison using compareToIgnoreCase method System.out.println(str1.compareToIgnoreCase(str2)); String str3 = "Happy"; String str4 = "hai"; System.out.println("\nComparison of str3 : " + str3 + " and str4 : " + str4); System.out.println("Using equals method "); if (str3.equals(str4)) { System.out.println("Both are equal"); } else { System.out.println("Both are not equal"); } System.out.println("Using compareTo method "); System.out.println(str3.compareTo(str4)); System.out.println("Using equalsIgnoreCase method "); System.out.println(str3.equalsIgnoreCase(str4)); System.out.println("Using compareToIgnoreCase method "); System.out.println(str3.compareToIgnoreCase(str4)); String str5 = "Happy"; String str6 = "hard"; System.out.println("\nComparison of str5 : " + str5 + " and str6 : " + str6); System.out.println("Using equals method "); if (str5.equals(str6)) { System.out.println("Both are equal"); } else { System.out.println("Both are not equal"); } System.out.println("Using compareTo method "); System.out.println(str5.compareTo(str6)); System.out.println("Using equalsIgnoreCase method "); System.out.println(str5.equalsIgnoreCase(str6)); System.out.println("Using compareToIgnoreCase method "); System.out.println(str5.compareToIgnoreCase(str6)); } }
输出:
字符串是字符序列,其对象是不可变的。有不同的方法可用于比较字符串,例如 equals、compareTo 等。所有这些方法都是根据要求使用的。它们在上一节中有详细解释。
以上是Java 中的字符串比较的详细内容。更多信息请关注PHP中文网其他相关文章!