1 / 39

สายอักขระ

สายอักขระ. หัวข้อ. สายอักขระ คลาส String คลาส StringBuffer. สายอักขระ. สายอักขระ. public class HelloWorld { public static void main ( String [] args ) { System . out . println ( " Hello, World ! 2 " ) ; } }. ประโยชน์ของสายอักขระ. บอกผลลัพธ์ของการคำนวณให้เราทราบ

zandra
Télécharger la présentation

สายอักขระ

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. สายอักขระ

  2. หัวข้อ • สายอักขระ • คลาส String • คลาส StringBuffer

  3. สายอักขระ

  4. สายอักขระ public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World! 2"); } }

  5. ประโยชน์ของสายอักขระ • บอกผลลัพธ์ของการคำนวณให้เราทราบ • รายงานขั้นตอนการทำงานของโปรแกรม • เก็บข้อมูลที่เป็นตัวอักษรตัวเลข สัญลักษณ์

  6. คลาส String

  7. การใช้งานสตริง • ประกาศตัวแปร String name; • กำหนดค่า name = new String("Smith"); • แสดงผล System.out.println(name);

  8. เรฟเฟอร์เรนซ์ (Reference) • int n • n เป็นข้อมูลชนิดจำนวนเต็ม • String name • ไม่ได้หมายความว่า name เป็นวัตถุ String • แต่เป็นการบอกว่า name เป็นเรฟเฟอร์เรนซ์ (reference) หรือตัวที่ใช้อ้างอิงไปที่วัตถุ String

  9. วัตถุและเรฟเฟอร์เรนซ์วัตถุและเรฟเฟอร์เรนซ์ • วัตถุ • เรฟเฟอร์เรนซ์

  10. การสั่งงานสตริง • รูปแบบ • เรฟเฟอร์เรนซ์.ข้อความ(สิ่งที่ส่งไปพร้อมกับข้อความ) • ตัวอย่าง • name.length(); • name.charAt(1);

  11. เมธอดที่น่าสนใจ • length() ความยาวของสตริง • charAt() ตัวอักษรในตำแหน่งที่กำหนด • ตัวอักษรตัวแรกคือตำแหน่งที่ 0 • ตัวอักษรสุดท้ายคือ length() - 1 • indexOf() ตำแหน่งของสายอักขระในสตริง • substring() สตริงที่อยู่ในช่วงที่กำหนด

  12. ตัวอย่างการใช้เมธอด public class StringMessage { public static void main(String[] args) { String name = "Smith"; System.out.println(name.length()); System.out.println(name.charAt(2)); System.out.println(name.indexOf("t")); System.out.println(name.substring(1, 3)); } }

  13. การเปรียบเทียบสตริง(แบบผิดๆ)การเปรียบเทียบสตริง(แบบผิดๆ) public class StringCompareInCorrect { public static void main(String[] args) { String name1 = new String(“Smith”); String name2 = new String(“Smith”); System.out.println(name1 == name2); } }

  14. การเปรียบเทียบสตริง(แบบผิดๆ)การเปรียบเทียบสตริง(แบบผิดๆ)

  15. การเปรียบเทียบสตริง public class StringCompare { public static void main(String[] args) { String name1 = new String(“Smith”); String name2 = new String(“Smith”); System.out.println(name1.equals(name2)); } }

  16. การต่อสตริง • เมธอด concat() • เครื่องหมาย + • เครื่องหมาย +=

  17. เมธอด concat() public class StringConcat1 { public static void main(String[] args) { String name = "Smith"; String lastName = " Brown"; String fullName =name.concat(lastname); System.out.println(fullName); } }

  18. เครื่องหมาย + public class StringConcat2 { public static void main(String[] args) { String name = "Smith"; String lastName = " Brown"; String fullName = name + lastName; System.out.println(fullName); } }

  19. เครื่องหมาย += public class StringAppend { public static void main(String[] args) { String name = "Smith"; String lastName = " Brown"; name += lastName; System.out.println(name); } }

  20. ความมั่นคงของสตริง (Immutability) • ไม่สามารถเปลี่ยนแปลงค่าของสายอักขระได้เลยหลังจากที่เราได้สร้างมันขึ้นมา • ในบรรดาข้อความ (message) ที่วัตถุ String รู้จักนั้น ไม่มีข้อความใดเลยที่ทำให้สายอักขระเปลี่ยนแปลงได้ • length() • substring()

  21. สตริงไม่เปลี่ยนแปลง String str; str = new String("Smith"); str = new String("John");

  22. สตริงไม่เปลี่ยนแปลง String str = "Smith"; System.out.println(str.toUpperCase()); System.out.println(str);

  23. สตริงไม่เปลี่ยนแปลง public class StringDeficiency { public static void main(String[] args) { String str = "A"; for (int i = 0; i < 100; i++) str += "A"; System.out.println(str); } }

  24. สตริงไม่เปลี่ยนแปลง

  25. คลาส StringBuffer

  26. ข้อดีของ String Buffer • ประหยัดทั้งหน่วยความจำและเวลาในการประมวลผล • วัตถุ String Buffer สามารถเปลี่ยนแปลงค่าในสายอักขระที่มันนำเสนอได้เอง โดยไม่ต้องสร้างวัตถุขึ้นมาใหม่

  27. เมธอดที่น่าสนใจ • append() การต่อสายอักขระ • insert() การแทรกสายอักขระ • delete() การลบสายอักขระย่อย

  28. ตัวอย่างการใช้เมธอด StringBuffer sb = new StringBuffer("John"); System.out.println(sb); sb.append(" Hunter"); System.out.println(sb); sb.insert(4, "y"); System.out.println(sb); sb.delete(2, 4); System.out.println(sb);

  29. ทดลองเปลี่ยนแปลง String Buffer public class StringBufferAppend { public static void main(String[] args) { StringBuffer sb; sb = new StringBuffer("A"); for (int i = 0; i < 100; i++) sb.append("A"); System.out.println(sb); } }

  30. ทดลองเปลี่ยนแปลง String Buffer

  31. String Builder • มีเมธอดที่เหมือนกับ String Buffer • ทำงานได้เร็วกว่า String Buffer • String Buffer ปลอดภัยสำหรับการทำงานในแบบ multiple threads

  32. Java Doc

  33. เอกสารอ้างอิง

  34. แหล่งเอกสาร • http://java.sun.com/j2se/1.5.0/docs/api • http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html • http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuffer.html

  35. สรุป

  36. สรุป • สายอักขระมีไว้เพื่อการแสดงผลข้อความ • สายอักขระได้ถูกนำเสนอด้วยวัตถุ String และ String Buffer • เราสามารถใช้งานวัตถุได้โดยผ่านทางเรฟเฟอร์เรนซ์ • เรฟเฟอร์เรนซ์เปรียบเสมือนรีโมทคอนโทรล • วัตถุเปรียบได้กับสิ่งที่รีโมทนั้นควบคุมอยู่ • การส่งข้อความหาวัตถุเปรียบได้กับการกดปุ่มรีโมทคอนโทรล

  37. สรุป • การเปรียบเทียบสตริงทำได้โดยการส่งข้อความ equals( ) • การต่อสตริงนอกจากจะใช้การส่งข้อความ concat() แล้ว ยังสามารถใช้เครื่องหมาย + ได้อีกด้วย • สายอักขระของวัตถุ String ไม่สามารถถูกเปลี่ยนแปลงได้ • ถ้าต้องการเปลี่ยนแปลงค่าในสายอักขระบ่อยๆ ควรใช้วัตถุ String Buffer

  38. แบบฝึกหัดท้ายหน่วยการเรียนรู้ที่ 4 1. สร้างวัตถุ String ขึ้นมาหนึ่งตัว ภายในบรรจุคำว่า "Sawasdee Thailand" แล้วแสดงสายอักขระนั้น 2. แสดงสายอักขระเหมือนในข้อ 1 แต่เปลี่ยนไปใช้วัตถุ String Buffer แทน 3. กำหนดข้อความเป็น http://www.abc.com/index.html ให้เขียนโปรแกรมที่พิมพ์ตำแหน่งของเครื่องหมาย / ทุกตัว

  39. แบบฝึกหัดท้ายหน่วยการเรียนรู้ที่ 4 4. สร้างวัตถุ String ขึ้นมา 2 ตัว และให้ทำการเปรียบเทียบด้วยเครื่องหมาย == แล้วให้ผลลัพธ์เป็นจริง 5. สร้างวัตถุ StringBuffer และทดสอบว่าการเปรียบเทียบด้วย เครื่องหมาย == และเมธอด equals() ให้ผลลัพธ์เหมือนหรือต่างกับวัตถุ String อย่างไรบ้าง

More Related