1. 文字列の比較

これは全く問題ありません。s1しかし、と の文字列は実際には同じであることがわかりますs2。これは、それらに同じテキストが含まれていることを意味します。String文字列を比較するときに、オブジェクトのアドレスではなくその内容を調べるようにプログラムに指示するにはどうすればよいでしょうか?

これを支援するために、Java のStringクラスにはequalsメソッドがあります。呼び出すと次のようになります。

string1.equals(string2)
2 つの文字列を比較する

このメソッドは、true文字列が同じであるかどうかを返します。また、false同じでない場合も返します。

例:

コード ノート
String s1 = "Hello";
String s2 = "HELLO";
String s3 = s1.toUpperCase();

System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s2.equals(s3));
// Hello
// HELLO
// HELLO

false // They are different
false // They are different
true // They are the same, even though the addresses are different

その他の例:

コード 説明
"Hello".equals("HELLO")
false
String s = "Hello";
"Hello".equals(s);
true
String s = "Hel";
"Hello".equals(s + "lo");
true
String s = "H";
(s + "ello").equals(s + "ello");
true

4
タスク
Java Syntax,  レベル 4レッスン 6
ロック未解除
Minimum of two numbers
All search and sort algorithms are based on comparisons. You'll be able to handle these very soon, if you so desire. In the meantime, we suggest starting with something small: write a program to find the minimum of two numbers. Find it and then display it. And if the numbers are the same, display either of them.
4
タスク
Java Syntax,  レベル 4レッスン 6
ロック未解除
Maximum of four numbers
Finding the maximum is an n-ary operation (an operation on n numbers) that returns the largest of several numbers. Never mind. We have no need for such definitions at the secret CodeGym center. We're here to learn how to write code. In this task, you need to use the keyboard to enter four numbers. Then determine the largest of them and display it on the screen.

2. 大文字と小文字を区別しない文字列比較

最後の例では、比較によりが得られることがわかりました。確かに、文字列は等しくありません。しかし..."Hello".equals("HELLO")false

明らかに、文字列は等しくありません。とはいえ、内容は同じ文字であり、大文字と小文字が異なるだけです。大文字と小文字を無視してそれらを比較する方法はありますか? つまり、次の結果が得られますか?"Hello".equals("HELLO")true

そして、この質問に対する答えは「はい」です。Java では、String型には別の特別なメソッドがありますequalsIgnoreCase。呼び出すと次のようになります。

string1.equalsIgnoreCase(string2)

このメソッドの名前は、大まかに訳すと、「比較するが、ケースを無視する」となります。メソッド名の文字には 2 つの垂直線が含まれています。1 つ目は小文字でL、2 つ目は大文字ですi。混乱しないでください。

例:

コード ノート
String s1 = "Hello";
String s2 = "HELLO";
String s3 = s1.toUpperCase();

System.out.println(s1.equalsIgnoreCase(s2));
System.out.println(s1.equalsIgnoreCase(s3));
System.out.println(s2.equalsIgnoreCase(s3));
// Hello
// HELLO
// HELLO

true
true
true

8
タスク
Java Syntax,  レベル 4レッスン 6
ロック未解除
Sorting three numbers
Planet Linear Chaos is populated by isomorphs. They are believed to have invented sorting algorithms. Everything in their heads is extremely well-ordered. They only issue planetary visas to people who know at least 7 sorting algorithms. Let's take our first step toward Linear Chaos: Read three numbers from the keyboard, put them in descending order, and then display them on the screen.

3. 文字列比較の例

簡単な例を 1 つだけ挙げてみましょう。キーボードから 2 行を入力し、それらが同じかどうかを判断する必要があるとします。コードは次のようになります。

Scanner console = new Scanner(System.in);
String a = console.nextLine();
String b = console.nextLine();
String result = a.equals(b) ? "Same" : "Different";
System.out.println(result);

4. 文字列比較の興味深いニュアンス

注意すべき重要なニュアンスが 1 つあります。

Java コンパイラがコード内 (特にコード内) で複数の同一の文字列を見つけた場合、メモリを節約するために、それらに対して 1 つのオブジェクトのみを作成します。

String text = "This is a very important message";
String message = "This is a very important message";

結果としてメモリに含まれる内容は次のとおりです。

文字列比較

ここで比較するとtext == message、 が得られますtrue。だから驚かないでください。

何らかの理由で参照を実際に異なるものにする必要がある場合は、次のように記述できます。

String text = "This is a very important message";
String message = new String ("This is a very important message");

またはこれ:

String text = "This is a very important message";
String message = new String (text);

どちらの場合も、textおよびmessage変数は、同じテキストを含む異なるオブジェクトを指します。


4
タスク
Java Syntax,  レベル 4レッスン 6
ロック未解除
Jen or Jen?
Jen, Company X's admin, learned how to pilot a space ship and flew away to another planet. People in Company X are good and sincere. It's just that they're scatterbrained and they mix up names. So they decided that the new administrator would also be called Jen. Let's help Company X find their Jen: write a program that checks the identity of two entered names.