200 likes | 321 Views
สตริง (String). ความหมายของสตริง. สตริง ( string) หรือ ข้อความ หมายถึง ชุด( array) ของตัวอักขระ( character) ที่เรียงต่อกัน การกำหนด string คือ การกำหนดอาร์เรย์ ของข้อมูลชนิด char หลาย ๆ ตัว นำมา เชื่อมต่อ กัน
E N D
ความหมายของสตริง • สตริง (string) หรือ ข้อความ หมายถึง ชุด(array)ของตัวอักขระ(character) ที่เรียงต่อกัน • การกำหนด string คือ การกำหนดอาร์เรย์ของข้อมูลชนิด char หลาย ๆ ตัวนำมาเชื่อมต่อกัน • 'C','o','m','p','u','t','e','r' เก็บไว้ในอาร์เรย์รวมเป็นข้อมูล string ซึ่งจะได้สตริงคือ "Computer"
Class String ข้อมูลประเภทสตริง หรือ ข้อความนั้น ในภาษาจาวาจะต้องสร้างออบเจ็กต์ของคลาสสตริง โดยใช้ keyword คำว่า new ในการสร้าง • String name1 = new String(“coffee”); • String name2; • name2 = new String (“Espresso”); มีข้อยกเว้นในคลาสนี้ ยกเว้นการใช้ new ได้ • String name1 = “coffee”; • String name2; • name2 = “Espresso”;
การเปลี่ยนอักขระ เป็นสตริง • char c = ‘a’; • String s; • int x =3; • s = c + “ ”; • System.out.print(s); • s = ‘s’+ “pin” • System.out.print(s);
Object และการอ้างอิง • การประกาศและให้ค่าตัวแปรแบบทั่วไป จะเป็นการจองพื้นที่ในหน่วยความจำแล้วนำค่าไปเก็บใส่พื้นที่บริเวณนั้นที่จองไว้ • แต่การประกาศและให้ค่า object จะเป็นการจองพื้นที่หน่วยความจำไว้สำหรับอ้างอิงไปยังพื้นที่หน่วยความจำอีกส่วนหนึ่งที่เก็บ object นั้นไว้
length() • หาจำนวนอักขระในสตริง return เป็นint String s1 = “Hello"; intlen = s1.length(); System.out.println(len);
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));
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());
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));
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);
trim() • จะตัดเว้นวรรค หรือ space ของสตริงออกไป return เป็นสตริงที่ไม่มีช่องว่างในข้อความ • String g1 = " Hello "; • String g2 = g1.trim(); • System.out.println("*" + g1 + "*"); • System.out.println('*' + g2 + '*');
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");
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));
indexOf() และlastIndexOf() String sentence= "I like computer programming"; intposition; 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 }
substring() • substring(int start) คืนค่าข้อความในสตริงตั้งแต่ตำแหน่ง start ในสตริง • substring(intstart, 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));
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.
valueOf() • double d = 102939939.939; • booleanb = true; • long l = 1232874; • 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) );
เปรียบเทียบสตริง • ใช้เครื่องหมาย == ตรวจสอบว่าสตริงสองตัวมีออบเจ็กต์ตัวเดียวกันหรือไม่ (เช็คจาก 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));
แบบฝึกหัด 1 • จงเขียนโปรแกรมตรวจสอบว่าสตริงสองค่าที่ผู้ใช้กรอกเข้ามานั้นเหมือนกันหรือไม่ โดยตัวอักษรพิมพ์เล็กหรือพิมพ์ใหญ่ตัวเดียวกัน ให้มองว่าเหมือนกัน
แบบฝึกหัด 2 • เขียนโปรแกรมรับsentence จากผู้ใช้ และ keyword ที่ผู้ใช้ต้องการค้นหา โดยตรวจสอบว่า sentence จากผู้ใช้นั้น พบ keyword ที่ต้องการค้นหากี่ครั้ง • Challenge! ลองเขียนโปรแกรมเพิ่มเติมว่าพบในตำแหน่งใดบ้าง