Java字符串compareTo()函数教程及示例

Java programming language provides the compareTo() function which is used to compare two strings. The comparison is done letter by letter and according to the situation, some results are returned by the compareTo()function. The comparison is done with the Unicode character set.

Java编程语言提供了compareTo()函数,该函数用于比较两个字符串。 比较是逐字母进行的,并且根据情况, compareTo()函数返回一些结果。 比较是使用Unicode字符集完成的。

compareTo()语法 (compareTo() Syntax)

First, we will learn the syntax of the compareTo() function as the return type, parameters, etc. compareTo() function has two main syntaxes where one accepts a string parameter and the other accepts an object type parameter.

首先,我们将学习compareTo()函数的语法作为返回类型,参数等。compareTo()函数有两种主要语法,其中一种接受字符串参数,另一种接受对象类型参数。

int compareTo(String str)
  • Here the return type fo the compareTo() function is int which will provide the comparison result as an integer.

    这里的compareTo()函数的返回类型是int ,它将以整数形式提供比较结果。

  • There is an only a single string which will be compared with the compareTo() function object like text.compareTo(myword)

    只有一个字符串将与compareTo()函数对象(例如text.compareTo(myword)

int compareTo(Object obj)
  • Here the return type fo the compareTo() function is int which will provide the comparison result as an integer.

    这里的compareTo()函数的返回类型是int ,它将以整数形式提供比较结果。

  • There is only a single object type parameter which will be converted to the string and compared with the compareTo() function like myobj.compareTo(yourobj)

    只有一个对象类型参数将被转换为字符串,并与诸如myobj.compareTo(yourobj)类的compareTo()函数进行比较

compareTo()返回值(compareTo() Return Values)

The most important part of the compareTo() function is the returned result. As we have learned compareTo() function returns an int or integer type value which expresses the comparison result. Let’s look following code which results will be like below.

compareTo()函数最重要的部分是返回的结果。 如我们所知,compareTo()函数返回一个int或整数类型值,该值表示比较结果。 让我们看下面的代码,结果将如下所示。

int result = myfirststring.compareTo(mysecondstring);
  • If result is 0 both strings are the same.

    如果result为0,则两个字符串相同。

  • If result is bigger than 0 myfirststring is bigger than mysecondstring.

    如果result大于0,则myfirststring大于mysecondstring

  • If result is lower than 0 mysecondstring is bigger than myfirststring.

    如果result小于0,则mysecondstring大于myfirststring

与字符串比较 (Compare with Strings)

Let’s work with the compareTo() function with different strings in examples. In these examples, we will compare the strings ABC , ABD , ACD with each other.

让我们在示例中使用带有不同字符串的compareTo()函数。 在这些示例中,我们将字符串ABCABDACD相互比较。

//CompareTo Examples In Java Programming Language 
import java.util.*;
public class CompareTo_Example
{
        public static void main(String[] args)
        {
                String s1="ABC";
                String s2="ABD";
                String s3="ACD";
                String s4="AAA";
                String s5="BBB";

                System.out.println(s1+" Compare To "+s1+" and Result is "+s1.compareTo(s1));
                System.out.println(s1+" Compare To "+s2+" and Result is "+s2.compareTo(s1));
                System.out.println(s1+" Compare To "+s3+" and Result is "+s3.compareTo(s1));
                System.out.println(s1+" Compare To "+s4+" and Result is "+s4.compareTo(s1));
                System.out.println(s1+" Compare To "+s5+" and Result is "+s5.compareTo(s1));

        }
}
Java字符串compareTo()函数教程及示例_第1张图片 Compare with Strings 与字符串比较

We will compile with the  javac and run the Java byte code like below.

我们将使用javac编译,并运行如下所示的Java字节码。

Java字符串compareTo()函数教程及示例_第2张图片

compareTo()函数区分大小写 (compareTo() Function Is Case Sensitive)

While comparing the string case sensitivity is very important. Case sensitive comparison will result in different for the same letter upper case and lower case like A and a. compareTo() function is also case sensitive by default because each letter has a different number for Unicode. We can use compareToIgnoreCase() function if we want to compare it in case-sensitively.

在比较字符串时,区分大小写非常重要。 区分大小写的比较将导致相同字母的大写和小写字母(例如A和a)不同。 默认情况下,compareTo()函数也区分大小写,因为每个字母的Unicode编号不同。 如果要区分大小写进行比较,可以使用compareToIgnoreCase()函数。

//CompareTo Examples In Java Programming Language 
import java.util.*;
public class CompareTo_Example
{
        public static void main(String[] args)
        {
                String s1="ABC";
                String s2="abc";
                String s3="Abc";

                System.out.println(s1+" Compare To "+s1+" and Result is "+s1.compareTo(s1));
                System.out.println(s1+" Compare To "+s2+" and Result is "+s2.compareTo(s1));
                System.out.println(s1+" Compare To "+s3+" and Result is "+s3.compareTo(s1));

        }
}
compareTo() Function Is Case Sensitive compareTo() Function Is Case Sensitive compareTo()函数区分大小写

compareToIgnoreCase()函数(compareToIgnoreCase() Function)

If we want to compare two strings as in case sensitive or by ignoring case sensitivity we can use comopareToIgnoreCase() function like below. The syntax and usage are the same as the compareTo() function.

如果我们想比较两个字符串是否区分大小写,或者忽略大小写,可以使用comopareToIgnoreCase()函数,如下所示。 语法和用法与compareTo()函数相同。

//CompareTo Examples In Java Programming Language 
import java.util.*;
public class CompareTo_Example
{
        public static void main(String[] args)
        {
                String s1="ABC";
                String s2="abc";
                String s3="Abc";

                System.out.println(s1+" Compare To "+s1+" and Result is "+s1.compareToIgnoreCase(s1));
                System.out.println(s1+" Compare To "+s2+" and Result is "+s2.compareToIgnoreCase(s1));
                System.out.println(s1+" Compare To "+s3+" and Result is "+s3.compareToIgnoreCase(s1));

        }
}
Java字符串compareTo()函数教程及示例_第3张图片 compareToIgnoreCase() Function compareToIgnoreCase()函数
compareToIgnoreCase() Function compareToIgnoreCase() Function compareToIgnoreCase()函数

As we can see all string compare operations provided which means equal with the case insensitive comparison.

如我们所见,提供了所有字符串比较操作这意味着与不区分大小写的比较相等。

LEARN MORE  How To Convert String To Int (Integer) In Java?
了解更多如何在Java中将字符串转换为Int(整数)?

翻译自: https://www.poftut.com/java-string-compareto-function-tutorial-with-examples/

你可能感兴趣的:(字符串,java,python,js,css,ViewUI)