の Java文字列クラスcompareTo() メソッドは、指定された文字列と現在の文字列を辞書編集的に比較します。正の数、負の数、または 0 を返します。
文字列内の各文字の Unicode 値に基づいて文字列を比較します。
最初の文字列が辞書編集上 2 番目の文字列より大きい場合、正の数 (文字値の差) が返されます。最初の文字列が辞書編集的に 2 番目の文字列より小さい場合は負の数を返し、最初の文字列が辞書編集的に 2 番目の文字列と等しい場合は 0 を返します。
if s1 > s2, it returns positive number if s1 <s2, 0 it returns negative number if s1="=" s2, < pre> <h3>Syntax</h3> <pre> public int compareTo(String anotherString) </pre> <p>The method accepts a parameter of type String that is to be compared with the current string.</p> <p>It returns an integer value. It throws the following two exceptions:</p> <p> <strong>ClassCastException:</strong> If this object cannot get compared with the specified object.</p> <p> <strong>NullPointerException:</strong> If the specified object is null.</p> <h2>Internal implementation</h2> <pre> int compareTo(String anotherString) { int length1 = value.length; int length2 = anotherString.value.length; int limit = Math.min(length1, length2); char v1[] = value; char v2[] = anotherString.value; int i = 0; while (i <limit) { char ch1="v1[i];" ch2="v2[i];" if (ch1 !="ch2)" return - ch2; } i++; length1 length2; < pre> <h2>Java String compareTo() Method Example</h2> <p> <strong>FileName:</strong> CompareToExample.java</p> <pre> public class CompareToExample{ public static void main(String args[]){ String s1='hello'; String s2='hello'; String s3='meklo'; String s4='hemlo'; String s5='flag'; System.out.println(s1.compareTo(s2));//0 because both are equal System.out.println(s1.compareTo(s3));//-5 because 'h' is 5 times lower than 'm' System.out.println(s1.compareTo(s4));//-1 because 'l' is 1 times lower than 'm' System.out.println(s1.compareTo(s5));//2 because 'h' is 2 times greater than 'f' }} </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 0 -5 -1 2 </pre> <h2>Java String compareTo(): empty string</h2> <p>When we compare two strings in which either first or second string is empty, the method returns the length of the string. So, there may be two scenarios:</p> <ul> <li>If <strong>first</strong> string is an empty string, the method returns a <strong>negative</strong> </li> <li>If <strong>second</strong> string is an empty string, the method returns a <strong>positive</strong> number that is the length of the first string.</li> </ul> <p> <strong>FileName:</strong> CompareToExample2.java</p> <pre> public class CompareToExample2{ public static void main(String args[]){ String s1='hello'; String s2=''; String s3='me'; System.out.println(s1.compareTo(s2)); System.out.println(s2.compareTo(s3)); }} </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 5 -2 </pre> <h3>Java String compareTo(): case sensitive</h3> <p>To check whether the compareTo() method considers the case sensitiveness of characters or not, we will make the comparison between two strings that contain the same letters in the same sequence.</p> <p>Suppose, a string having letters in uppercase, and the second string having the letters in lowercase. On comparing these two string, if the outcome is 0, then the compareTo() method does not consider the case sensitiveness of characters; otherwise, the method considers the case sensitiveness of characters.</p> <p> <strong>FileName:</strong> CompareToExample3.java</p> <pre> public class CompareToExample3 { // main method public static void main(String argvs[]) { // input string in uppercase String st1 = new String('INDIA IS MY COUNTRY'); // input string in lowercase String st2 = new String('india is my country'); System.out.println(st1.compareTo(st2)); } } </pre> <p> <strong>Output:</strong> </p> <pre> -32 </pre> <p> <strong>Conclusion:</strong> It is obvious by looking at the output that the outcome is not equal to zero. Hence, the compareTo() method takes care of the case sensitiveness of characters.</p> <h3>Java String compareTo(): ClassCastException</h3> <p>The <strong>ClassCastException</strong> is thrown when objects of incompatible types get compared. In the following example, we are comparing an object of the ArrayList (al) with a string literal ('Sehwag').</p> <p> <strong>FileName:</strong> CompareToExample4.java</p> <pre> // import statement import java.util.*; class Players { private String name; // constructor of the class public Players(String str) { name = str; } } public class CompareToExample4 { // main method public static void main(String[] args) { Players ronaldo = new Players('Ronaldo'); Players sachin = new Players('Sachin'); Players messi = new Players('Messi'); ArrayList al = new ArrayList(); al.add(ronaldo); al.add(sachin); al.add(messi); // performing binary search on the list al Collections.binarySearch(al, 'Sehwag', null); } } </pre> <p> <strong>Output:</strong> </p> <pre> Exception in thread 'main' java.lang.ClassCastException: class Players cannot be cast to class java.lang.Comparable </pre> <h3>Java String compareTo(): NullPointerException</h3> <p>The NullPointerException is thrown when a null object invokes the compareTo() method. Observe the following example.</p> <p> <strong>FileName:</strong> CompareToExample5.java</p> <pre> public class CompareToExample5 { // main method public static void main(String[] args) { String str = null; // null is invoking the compareTo method. Hence, the NullPointerException // will be raised int no = str.compareTo('India is my country.'); System.out.println(no); } } </pre> <p> <strong>Output:</strong> </p> <pre> Exception in thread 'main' java.lang.NullPointerException at CompareToExample5.main(CompareToExample5.java:9) </pre> <hr></limit)></pre></s2,>
このメソッドは、現在の文字列と比較される String 型のパラメータを受け入れます。
整数値を返します。次の 2 つの例外がスローされます。
クラスキャスト例外: このオブジェクトが指定されたオブジェクトと比較できない場合。
NullPointerException: 指定されたオブジェクトが null の場合。
Java文字列の追加
内部実装
int compareTo(String anotherString) { int length1 = value.length; int length2 = anotherString.value.length; int limit = Math.min(length1, length2); char v1[] = value; char v2[] = anotherString.value; int i = 0; while (i <limit) { char ch1="v1[i];" ch2="v2[i];" if (ch1 !="ch2)" return - ch2; } i++; length1 length2; < pre> <h2>Java String compareTo() Method Example</h2> <p> <strong>FileName:</strong> CompareToExample.java</p> <pre> public class CompareToExample{ public static void main(String args[]){ String s1='hello'; String s2='hello'; String s3='meklo'; String s4='hemlo'; String s5='flag'; System.out.println(s1.compareTo(s2));//0 because both are equal System.out.println(s1.compareTo(s3));//-5 because 'h' is 5 times lower than 'm' System.out.println(s1.compareTo(s4));//-1 because 'l' is 1 times lower than 'm' System.out.println(s1.compareTo(s5));//2 because 'h' is 2 times greater than 'f' }} </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 0 -5 -1 2 </pre> <h2>Java String compareTo(): empty string</h2> <p>When we compare two strings in which either first or second string is empty, the method returns the length of the string. So, there may be two scenarios:</p> <ul> <li>If <strong>first</strong> string is an empty string, the method returns a <strong>negative</strong> </li> <li>If <strong>second</strong> string is an empty string, the method returns a <strong>positive</strong> number that is the length of the first string.</li> </ul> <p> <strong>FileName:</strong> CompareToExample2.java</p> <pre> public class CompareToExample2{ public static void main(String args[]){ String s1='hello'; String s2=''; String s3='me'; System.out.println(s1.compareTo(s2)); System.out.println(s2.compareTo(s3)); }} </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 5 -2 </pre> <h3>Java String compareTo(): case sensitive</h3> <p>To check whether the compareTo() method considers the case sensitiveness of characters or not, we will make the comparison between two strings that contain the same letters in the same sequence.</p> <p>Suppose, a string having letters in uppercase, and the second string having the letters in lowercase. On comparing these two string, if the outcome is 0, then the compareTo() method does not consider the case sensitiveness of characters; otherwise, the method considers the case sensitiveness of characters.</p> <p> <strong>FileName:</strong> CompareToExample3.java</p> <pre> public class CompareToExample3 { // main method public static void main(String argvs[]) { // input string in uppercase String st1 = new String('INDIA IS MY COUNTRY'); // input string in lowercase String st2 = new String('india is my country'); System.out.println(st1.compareTo(st2)); } } </pre> <p> <strong>Output:</strong> </p> <pre> -32 </pre> <p> <strong>Conclusion:</strong> It is obvious by looking at the output that the outcome is not equal to zero. Hence, the compareTo() method takes care of the case sensitiveness of characters.</p> <h3>Java String compareTo(): ClassCastException</h3> <p>The <strong>ClassCastException</strong> is thrown when objects of incompatible types get compared. In the following example, we are comparing an object of the ArrayList (al) with a string literal ('Sehwag').</p> <p> <strong>FileName:</strong> CompareToExample4.java</p> <pre> // import statement import java.util.*; class Players { private String name; // constructor of the class public Players(String str) { name = str; } } public class CompareToExample4 { // main method public static void main(String[] args) { Players ronaldo = new Players('Ronaldo'); Players sachin = new Players('Sachin'); Players messi = new Players('Messi'); ArrayList al = new ArrayList(); al.add(ronaldo); al.add(sachin); al.add(messi); // performing binary search on the list al Collections.binarySearch(al, 'Sehwag', null); } } </pre> <p> <strong>Output:</strong> </p> <pre> Exception in thread 'main' java.lang.ClassCastException: class Players cannot be cast to class java.lang.Comparable </pre> <h3>Java String compareTo(): NullPointerException</h3> <p>The NullPointerException is thrown when a null object invokes the compareTo() method. Observe the following example.</p> <p> <strong>FileName:</strong> CompareToExample5.java</p> <pre> public class CompareToExample5 { // main method public static void main(String[] args) { String str = null; // null is invoking the compareTo method. Hence, the NullPointerException // will be raised int no = str.compareTo('India is my country.'); System.out.println(no); } } </pre> <p> <strong>Output:</strong> </p> <pre> Exception in thread 'main' java.lang.NullPointerException at CompareToExample5.main(CompareToExample5.java:9) </pre> <hr></limit)>今すぐテストしてください
出力:
0 -5 -1 2
Java String CompareTo(): 空の文字列
最初または 2 番目の文字列のいずれかが空である 2 つの文字列を比較すると、メソッドは文字列の長さを返します。したがって、次の 2 つのシナリオが考えられます。
- もし 初め string が空の文字列の場合、メソッドは ネガティブ
- もし 2番 string が空の文字列の場合、メソッドは ポジティブ 最初の文字列の長さを示す数値。
ファイル名: CompareToExample2.java
public class CompareToExample2{ public static void main(String args[]){ String s1='hello'; String s2=''; String s3='me'; System.out.println(s1.compareTo(s2)); System.out.println(s2.compareTo(s3)); }}今すぐテストしてください
出力:
5 -2
Java String CompareTo(): 大文字と小文字を区別します
CompareTo() メソッドが文字の大文字と小文字の区別を考慮しているかどうかを確認するために、同じ順序で同じ文字を含む 2 つの文字列を比較します。
文字列が大文字で構成され、2 番目の文字列が小文字で構成されているとします。これら 2 つの文字列を比較して、結果が 0 の場合、compareTo() メソッドは文字の大文字と小文字の区別を考慮しません。それ以外の場合、メソッドは文字の大文字と小文字の区別を考慮します。
ファイル名: CompareToExample3.java
ローマ数字 1 から 100
public class CompareToExample3 { // main method public static void main(String argvs[]) { // input string in uppercase String st1 = new String('INDIA IS MY COUNTRY'); // input string in lowercase String st2 = new String('india is my country'); System.out.println(st1.compareTo(st2)); } }
出力:
-32
結論: 出力を見ると、結果がゼロではないことが明らかです。したがって、compareTo() メソッドは文字の大文字と小文字の区別を考慮します。
Java String CompareTo(): ClassCastException
の クラスキャスト例外 互換性のない型のオブジェクトが比較されるとスローされます。次の例では、ArrayList のオブジェクト (al) と文字列リテラル ('Sehwag') を比較しています。
ファイル名: CompareToExample4.java
// import statement import java.util.*; class Players { private String name; // constructor of the class public Players(String str) { name = str; } } public class CompareToExample4 { // main method public static void main(String[] args) { Players ronaldo = new Players('Ronaldo'); Players sachin = new Players('Sachin'); Players messi = new Players('Messi'); ArrayList al = new ArrayList(); al.add(ronaldo); al.add(sachin); al.add(messi); // performing binary search on the list al Collections.binarySearch(al, 'Sehwag', null); } }
出力:
Exception in thread 'main' java.lang.ClassCastException: class Players cannot be cast to class java.lang.Comparable
Java String CompareTo(): NullPointerException
NullPointerException は、null オブジェクトが CompareTo() メソッドを呼び出すとスローされます。次の例を見てください。
ファイル名: CompareToExample5.java
public class CompareToExample5 { // main method public static void main(String[] args) { String str = null; // null is invoking the compareTo method. Hence, the NullPointerException // will be raised int no = str.compareTo('India is my country.'); System.out.println(no); } }
出力:
Exception in thread 'main' java.lang.NullPointerException at CompareToExample5.main(CompareToExample5.java:9)