Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

การใช้งาน String Utility และ Immutability ใน JShell Java 9

JShell เป็นเครื่องมือ command-line แบบโต้ตอบ ใช้ implement คำสั่งง่ายๆ เช่น expressions, classes, methods, fields, interfaces เป็นต้น String class เป็นส่วนหนึ่งของ built-in java.lang package มีเมธอดมากมายสำหรับ text processing

1) String Utility Methods

String มี built-in utility methods มากมาย เช่น indexOf(), lastIndexOf(), startsWith(), endsWith(), isEmpty(), equals(), equalsIgnoreCase()

ตัวอย่างใน JShell

jshell> String str = "JShell is a new feature in Java9";
str ==> "JShell is a new feature in Java9"

jshell> str.indexOf("new")
$4 ==> 12

jshell> str.charAt(7)
$5 ==> 'i'

jshell> str.indexOf('i')
$6 ==> 7

jshell> str.lastIndexOf('i')
$7 ==> 24

jshell> str.contains("feature")
$8 ==> true

jshell> str.startsWith("JShell")
$9 ==> true

jshell> str.startsWith("Java9")
$10 ==> false

jshell> str.endsWith("Java9")
$11 ==> true

jshell> str.endsWith("a9")
$12 ==> true

jshell> str.endsWith("a9java")
$13 ==> false

jshell> String str1 = "value"
str1 ==> "value"

jshell> str1.equals("value")
$15 ==> true

jshell> str1.equals("VALUE")
$16 ==> false

jshell> str1.equalsIgnoreCase("VALUE")
$17 ==> true

2) String Immutability

String objects เป็น immutable หมายถึงไม่สามารถเปลี่ยนค่าได้หลังสร้างแล้ว

ตัวอย่างใน JShell

jshell> String str = "Tutorialspoint";
str ==> "Tutorialspoint"

jshell> str.concat(" is e-learning app");
$3 ==> "Tutorialspoint is e-learning app"

jshell> str
str ==> "Tutorialspoint"  // ค่าเดิมไม่เปลี่ยน!

jshell> String str1 = str.concat(".");
str1 ==> "Tutorialspoint."

jshell> str1
str1 ==> "Tutorialspoint."

jshell> String str = str.concat(" is e-learning app");  // reassign
str ==> "Tutorialspoint is e-learning app"

jshell> str
str ==> "Tutorialspoint is e-learning app"

jshell> String str1 = "Tutorialspoint";
str1 ==> "Tutorialspoint"

jshell> str1.concat(" is e-learning app");
$2 ==> "Tutorialspoint is e-learning app"

jshell> str1
str1 ==> "Tutorialspoint"  // ยังคงค่าเดิม

jshell> String str2 = str1.concat(" is e-learning app");
str2 ==> "Tutorialspoint is e-learning app"

jshell> str1
str1 ==> "Tutorialspoint"

jshell> String str3 = str2.concat(".");
str3 ==> "Tutorialspoint is e-learning app."

jshell> str1
str1 ==> "Tutorialspoint"

jshell> str2
str2 ==> "Tutorialspoint is e-learning app"

jshell> String s = "Tutorialspoint is e-learning app.";
s ==> "Tutorialspoint is e-learning app."

jshell> s.toUpperCase()
$10 ==> "TUTORIALSPOINT IS E-LEARNING APP."

jshell> s.toLowerCase()
$11 ==> "tutorialspoint is e-learning app."

สรุปเมธอดสำคัญที่สร้าง String ใหม่

เมธอด คำอธิบาย ตัวอย่าง
concat() ต่อสตริง s1.concat(s2)
toUpperCase() แปลงเป็นตัวพิมพ์ใหญ่ s.toUpperCase()
toLowerCase() แปลงเป็นตัวพิมพ์เล็ก s.toLowerCase()
trim() ตัด whitespace หน้า/หลัง s.trim()
replace() แทนที่ character/sequence s.replace('a', 'b')
substring() ตัด substring s.substring(0, 5)

Best Practices

  • ใช้ StringBuilder สำหรับการต่อสตริงหลายครั้งใน loop
  • ใช้ equals() แทน == สำหรับเปรียบเทียบเนื้อหา
  • ระวัง null pointer - ใช้ Objects.equals() หรือ "literal".equals(var)
  • String literals ถูกเก็บใน String pool - ใช้ intern() หากต้องการ