1 / 15

이해와 활용: 자바 문자열 클래스의 모든 것

이 문서에서는 자바의 문자열(String) 클래스에 대한 기본적인 이해와 다양한 활용 방법을 소개합니다. 문자열 클래스는 자바에서 매우 중요한 역할을 하며, 여러 유용한 메서드를 제공합니다. 이 글에서는 문자열의 생성, 자르기, 대소문자 변환, 연결, 비교 등 여러 기능을 코드 예제와 함께 설명합니다. 이를 통해 자바 프로그래밍에서 문자열을 효과적으로 사용할 수 있는 방법을 배울 수 있습니다.

Download Presentation

이해와 활용: 자바 문자열 클래스의 모든 것

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. การโปรแกรมเชิงวัตถุด้วยภาษา JAVA String Class มหาวิทยาลัยเนชั่น http://www.nation.ac.th บุรินทร์ รุจจนพันธุ์ . ปรับปรุง 21 มิถุนายน 2550

  2. ความหมายของ String String เป็น Class ที่ถูกเรียกเข้ามาใช้ในจาวาทันที มี Method ที่น่าใช้มากมาย และไม่ต้อง Import เข้าไปเหมือน Class อื่น เพราะ extends Object เข้ามา public final class String ข้อมูลจาก http://www.yonok.ac.th/pmy/j2sdk-1_4_2-doc.zip ข้อมูลจาก http://yn1.yonok.ac.th/burin/javadocs/api/java/lang/String.html

  3. ตัวอย่างการใช้ String DOS>java x aa class x { public static void main(String args[]){ System.out.println(args[0]); //aa } } class y { public static void main(String[] a){ System.out.println(a.length); } }

  4. String คือ Array of Character String x = "abc"; char data[] = {'a','b','c'}; String y = new String(data);

  5. การใช้ String 5 แบบอย่างง่าย 1. System.out.println("abc"); 2. String cde = "cde"; 3. System.out.println("abc" + cde); 4. String c = "abc".substring(2,3); 5. String d = cde.substring(1,2); First Started at 0 Last Started at 1

  6. พิมพ์ใหญ่ พิมพ์เล็ก และ Substring String z ="ThaiAll"; System.out.println("string = " + z); System.out.println(z.substring(0,4)); // Thai System.out.println(z.substring(2,5)); // aiA System.out.println(z.substring(4)); // All System.out.println(z.toUpperCase()); // THAIALL System.out.println(z.toLowerCase()); // thaiall

  7. พิมพ์ 1 ถึง 10 และ 10 ถึง 1 for(int I=1;I<=10;I++){ System.out.println(I); } for(int I=10;I>=1;I--){ System.out.println(I); }

  8. ตัดตัวอักษรออกมาจากข้อความ (subString) class x{ public static void main(String[] a){ String s = "abcd"; for(int I=0;I<s.length();I++){ System.out.print(s.substring(I,I+1)); } } } Output abcd

  9. String เป็น Array of Char (toCharArray) class x{ public static void main(String[] a){ String s = "abcd"; char ar[]; ar = s.toCharArray(); for(int I=0;I<ar.length;I++){ System.out.print(ar[I]); } } } Output abcd

  10. ตัดข้อความตามตำแหน่ง (subSequence) Returns a new character sequence that is a subsequence of this sequence. class x{ public static void main(String[] a){ String s = "burin and thaiall"; //17 int ln = s.length() - 3; System.out.println(s.subSequence(2,ln)); } } Output rin and thai

  11. แยกข้อความออกจากกัน (split) class x{ public static void main(String[] a){ String s = "cat dog boy"; String ar[] = s.split(" "); for(int I=0;I<ar.length;I++){ System.out.print(ar[I]); } } } Output catdogboy

  12. แทนที่ช่องว่างด้วยคอมม่า (replace) class x{ public static void main(String[] a){ String s = "cat dog boy"; String n = s.replace(' ',','); System.out.print(n); } } Output cat,dog,boy

  13. ต่อข้อความเข้าด้วยกัน (concat) class x{ public static void main(String[] a){ String s = "boy"; String n = s.concat("dog"); n = n + "cat"; System.out.print(n); } } Output boydogcat

  14. เปรียบเทียบ (equals) class x{ public static void main(String[] a){ String s = "cat"; if (s.equals("cat")) System.out.print(1); if (s.equalsIgnoreCase("CAT")) System.out.print(2); } } Output 12

  15. ดึงตัวอักษรจากข้อความ (charAt) class x{ public static void main(String[] a){ String s = "abcd"; char c = s.charAt(2); System.out.print(s.charAt(0)); System.out.print(s.charAt(1) + 0); System.out.print(c); } } Output a98c

More Related