1. การเปรียบเทียบสตริง

ทั้งหมดนี้เป็นสิ่งที่ดีและดี แต่คุณจะเห็นว่าจริง ๆ แล้วสตริงs1และs2สตริงนั้นเหมือนกัน หมายความว่ามีข้อความเดียวกัน เมื่อเปรียบเทียบสตริง คุณจะบอกโปรแกรมอย่างไรไม่ให้ดูที่อยู่ของ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,  ระดับบทเรียน
ล็อค
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,  ระดับบทเรียน
ล็อค
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)

ชื่อของเมธอดแปลประมาณว่าเปรียบเทียบ แต่ไม่สนใจ case ตัวอักษรในชื่อเมธอดมีเส้นแนวตั้งสองเส้น เส้นแรกเป็นตัวพิมพ์เล็กและ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,  ระดับบทเรียน
ล็อค
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);

ในทั้งสองกรณีนี้ ตัวแปร textและmessageชี้ไปที่วัตถุต่างๆ ที่มีข้อความเดียวกัน


4
งาน
Java Syntax,  ระดับบทเรียน
ล็อค
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.