Ics3u stringbuffer
Download
1 / 11

ICS3U – StringBuffer - PowerPoint PPT Presentation


  • 85 Views
  • Uploaded on
  • Presentation posted in: General

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


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

  • Another special data type (or class) in Java

  • Like String, except that the value of StringBuffer can be changed.


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


sb.length()

In the main() method:

StringBuffer sb = new StringBuffer(“Hello”);

System.out.println(sb.length());

Output:

5


sb.charAt(1)

In the main() method:

StringBuffer sb = new StringBuffer(“Hello”);

System.out.println(sb.charAt(1));

Output:

e


sb.insert(2, “ABC”)

In the main() method:

StringBuffer sb = new StringBuffer(“Hello”);

System.out.println(sb.insert(2, “ABC”));

Output:

HeABCllo


sb.append(“DEF”)

In the main() method:

StringBuffer sb = new StringBuffer(“Hello”);

System.out.println(sb.append(“DEF”));

Output:

HelloDEF


sb.reverse()

In the main() method:

StringBuffer sb = new StringBuffer(“Hello”);

System.out.println(sb.reverse());

Output:

olleH


sb.toString()

In the main() method:

StringBuffer sb = new StringBuffer(“Hello”);

String s = sb.toString();

System.out.println(s);

Output:

Hello


Classwork

  • Go to http://computerNHSS.wikispaces.com

  • Download the classwork:

    • JBB_pg_18.pdf

    • JBB_pg_19.pdf

    • JBB_pg_22.pdf


  • Login