1.比較字符串

這一切都很好。但是你可以看到 thes1s2strings 實際上是相同的,這意味著它們包含相同的文本。比較字符串時,如何告訴程序不要看String對象的地址,而是看它們的內容?

為了幫助我們解決這個問題,Java 的String類有equals方法。調用它看起來像這樣:

string1.equals(string2)
比較兩個字符串

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)

該方法的名稱大致翻譯為比較但忽略大小寫。方法名稱中的字母包括兩條豎線:第一條是小寫字母L,第二條是大寫字母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.字符串比較示例

舉一個簡單的例子:假設你需要從鍵盤輸入兩行,並判斷它們是否相同。這就是代碼的樣子:

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. 字符串比較的一個有趣的細微差別

您需要注意一個重要的細微差別。

如果Java 編譯器在您的代碼中(特別是在您的代碼中)發現多個相同的字符串,那麼它將只為它們創建一個對像以節省內存。

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);

在這兩種情況下,textmessage變量指向包含相同文本的不同對象。


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.