งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

สตริง (String).

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "สตริง (String)."— ใบสำเนางานนำเสนอ:

1 สตริง (String)

2 ความหมายของสตริง สตริง (string) หรือ ข้อความ หมายถึง ชุด(array)ของตัวอักขระ(character) ที่เรียงต่อ กัน การกำหนด string คือ การกำหนดอาร์เรย์ของข้อมูลชนิด char หลาย ๆ ตัวนำมา เชื่อมต่อกัน 'C','o','m','p','u','t','e','r' เก็บไว้ในอาร์เรย์รวมเป็นข้อมูล string ซึ่งจะได้สตริงคือ "Computer" [0] C [1] O [2] M [3] P [4] U [5] T [6] E [7] R

3 Class String ข้อมูลประเภทสตริง หรือ ข้อความนั้น ในภาษาจาวาจะต้องสร้างออบเจ็กต์ของคลาส สตริง โดยใช้ keyword คำว่า new ในการสร้าง String name1 = new String(“coffee”); String name2; name2 = new String (“Espresso”); มีข้อยกเว้นในคลาสนี้ ยกเว้นการใช้ new ได้ String name1 = “coffee”; name2 = “Espresso”;

4 การเปลี่ยนอักขระ เป็นสตริง
char c = ‘a’; String s; int x =3; s = c + “ ”; System.out.print(s); s = ‘s’+ “pin”

5 Object และการอ้างอิง การประกาศและให้ค่าตัวแปรแบบทั่วไป จะเป็นการจองพื้นที่ในหน่วยความจำแล้ว นำค่าไปเก็บใส่พื้นที่บริเวณนั้นที่จองไว้ แต่การประกาศและให้ค่า object จะเป็นการจองพื้นที่หน่วยความจำไว้สำหรับอ้างอิง ไปยังพื้นที่หน่วยความจำอีกส่วนหนึ่งที่เก็บ object นั้นไว้

6 length() หาจำนวนอักขระในสตริง return เป็น int String s1 = “Hello";
int len = s1.length(); System.out.println(len);

7 charAt(int index) หาตัวอักขระในตำแหน่ง index ที่ระบุในสตริง return เป็นตัวอักขระที่พบ (char) String s1 = "Hello"; System.out.println(s1.charAt(0)); System.out.println(s1.charAt(3)); System.out.println(s1.charAt(5));

8 toUpperCase() และ toLowerCase()
toUpperCase() เปลี่ยนข้อความสตริงให้เป็นตัวใหญ่ทั้งหมด return เป็น String toLowerCase() เปลี่ยนข้อความสตริงให้เป็นตัวเล็กทั้งหมด return เป็น String String s1 = “Hello"; String s2 = "WorlD"; s1 = s1.toUpperCase(); System.out.println(s1); System.out.println(s2.toLowerCase());

9 concat(String str) นำข้อความสตริง str ไปต่อท้ายสตริงที่กำหนด return เป็นสตริงที่ต่อกันแล้ว String fullname; String first = "John"; String last = "Terry"; fullname = first.concat(last); System.out.println(fullname); System.out.println("Hello ".concat("Mr. ").concat(fullname));

10 replace(char oldchar, char newchar) และ replaceAll(String oldstr, String newstr)
replace : แทนค่าตัวอักขระ oldchar ด้วยอักขระ newchar ในสตริงที่กำหนด replaceAll : แทนค่าสตริง oldstr ด้วยสตริง newstr ในสตริงที่กำหนด String str1 = "Tom tried to get in the train"; String str2,str3; str2 = str1.replace('t','D'); str3 = str1.replaceAll("tr","aaa"); System.out.println(str2); System.out.println(str3);

11 trim() จะตัดเว้นวรรค หรือ space ของสตริงออกไป return เป็นสตริงที่ไม่มีช่องว่างในข้อความ String g1 = " Hello "; String g2 = g1.trim(); System.out.println("*" + g1 + "*"); System.out.println('*' + g2 + '*');

12 startsWith(String str) และ endsWith(String str)
startsWith(String str) คืนค่าเป็นจริง หากสตริงที่กำหนดเริ่มต้นด้วยสตริง str endsWith(String str) คืนค่าเป็นจริง หากสตริงที่กำหนดลงท้ายด้วยสตริง str String a = "I met you seven years ago."; if (a.startsWith("Im")) System.out.println("begin with I"); else System.out.println("does not begin with I");

13 indexOf() และ lastIndexOf()
ใช้ค้นหาตัวอักขระหรือสตริง ในข้อความที่ต้องการ return ค่าเป็นตำแหน่ง index ที่พบ String target = "banana mango"; System.out.println(target.indexOf('n')); System.out.println(target.indexOf("an")); System.out.println(target.indexOf(‘n’,6)); System.out.println(target.indexOf("an",5)); System.out.println(target.indexOf('e')); System.out.println(target.indexOf("x")); System.out.println(target.lastIndexOf('n')); System.out.println(target.lastIndexOf("an")); System.out.println(target.lastIndexOf(‘n’,6)); System.out.println(target.lastIndexOf("an",5));

14 indexOf() และ lastIndexOf()
String sentence= "I like computer programming"; int position; System.out.println("letter r appears at "); position = sentence.indexOf('r',0); //หา r ตั้งแต่ index ที่ 0 while (position != -1) { System.out.println(position); position = sentence.indexOf('r',++position); //หา r ต่อโดยขยับ index }

15 substring() substring(int start) คืนค่าข้อความในสตริงตั้งแต่ตำแหน่ง start ในสตริง substring(int start, int end) คืนค่าข้อความในสตริงตั้งแต่ตำแหน่ง start ถึง end ใน สตริง String name = "Rajamangala University of Technology Tawan-ok"; System.out.println(name.substring(14)); System.out.println(name.substring(4,10));

16 valueOf() เปลี่ยนข้อมูลแบบต่างๆ ให้กลายเป็นสตริง
valueOf(boolean b): Returns the string representation of the boolean argument. valueOf(char c) : Returns the string representation of the char argument. valueOf(char[] data) : Returns the string representation of the char array argument. valueOf(double d) : Returns the string representation of the double argument. valueOf(float f) : Returns the string representation of the float argument. valueOf(int i) : Returns the string representation of the int argument. valueOf(long l) : Returns the string representation of the long argument. valueOf(Object obj) : Returns the string representation of the Object argument.

17 valueOf() double d = 102939939.939; boolean b = true;
long l = ; char[] arr = {'a', 'b', 'c', 'd', 'e', 'f','g' }; System.out.println("Return Value : " + String.valueOf(d) ); System.out.println("Return Value : " + String.valueOf(b) ); System.out.println("Return Value : " + String.valueOf(l) ); System.out.println("Return Value : " + String.valueOf(arr) );

18 เปรียบเทียบสตริง ใช้เครื่องหมาย == ตรวจสอบว่าสตริงสองตัวมีออบเจ็กต์ตัวเดียวกันหรือไม่ (เช็คจาก addr ของสตริง) ใช้คำสั่ง equals() และ compareTo() ตรวจสอบสตริงจากค่าของข้อมูล (ไม่เช็คจาก address) String word1, word2; word1 = "Java"; word2 = "Java"; System.out.println(word1==word2); System.out.println(word1.equals(word2)); System.out.println(word1.compareTo(word2)); String word3, word4; word3 = new String("Java"); word4 = new String("Java"); System.out.println(word3==word4); System.out.println(word3.equals(word4)); System.out.println(word3.compareTo(word4));

19 แบบฝึกหัด 1 จงเขียนโปรแกรมตรวจสอบว่าสตริงสองค่าที่ผู้ใช้กรอกเข้ามานั้นเหมือนกันหรือไม่ โดยตัวอักษรพิมพ์เล็กหรือพิมพ์ใหญ่ตัวเดียวกัน ให้มองว่าเหมือนกัน

20 แบบฝึกหัด 2 เขียนโปรแกรมรับ sentence จากผู้ใช้ และ keyword ที่ผู้ใช้ต้องการค้นหา โดย ตรวจสอบว่า sentence จากผู้ใช้นั้น พบ keyword ที่ต้องการค้นหากี่ครั้ง Challenge! ลองเขียนโปรแกรมเพิ่มเติมว่าพบในตำแหน่งใดบ้าง


ดาวน์โหลด ppt สตริง (String).

งานนำเสนอที่คล้ายกัน


Ads by Google