1 / 11

ICS3U – StringBuffer

ICS3U – StringBuffer. Teacher: Mr. Ho Course URL: http://computerNHSS.wikispaces.com. Review of String. String is a special data type (or class) in Java E.g. String str1 = “Hello, I am a string”; Once str1 is assigned a value, its value cannot be changed. StringBuffer.

judah
Download Presentation

ICS3U – StringBuffer

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. ICS3U – StringBuffer Teacher: Mr. Ho Course URL: http://computerNHSS.wikispaces.com

  2. Review of String • String is a special data type (or class) in Java • E.g. String str1 = “Hello, I am a string”; • Once str1 is assigned a value, its value cannot be changed.

  3. StringBuffer • Another special data type (or class) in Java • Like String, except that the value of StringBuffer can be changed.

  4. Built-In Methods • StringBuffer sb = new StringBuffer(“Hello”); • sb.length(); // length of StringBuffer • sb.charAt(1); // getting a character • sb.insert(2, “ABC”); // inserting • sb.append(“DEF”); // appending • sb.reverse(); // reversing StringBuffer • String s = sb.toString(); // getting String

  5. sb.length() In the main() method: StringBuffer sb = new StringBuffer(“Hello”); System.out.println(sb.length()); Output: 5

  6. sb.charAt(1) In the main() method: StringBuffer sb = new StringBuffer(“Hello”); System.out.println(sb.charAt(1)); Output: e

  7. sb.insert(2, “ABC”) In the main() method: StringBuffer sb = new StringBuffer(“Hello”); System.out.println(sb.insert(2, “ABC”)); Output: HeABCllo

  8. sb.append(“DEF”) In the main() method: StringBuffer sb = new StringBuffer(“Hello”); System.out.println(sb.append(“DEF”)); Output: HelloDEF

  9. sb.reverse() In the main() method: StringBuffer sb = new StringBuffer(“Hello”); System.out.println(sb.reverse()); Output: olleH

  10. sb.toString() In the main() method: StringBuffer sb = new StringBuffer(“Hello”); String s = sb.toString(); System.out.println(s); Output: Hello

  11. Classwork • Go to http://computerNHSS.wikispaces.com • Download the classwork: • JBB_pg_18.pdf • JBB_pg_19.pdf • JBB_pg_22.pdf

More Related