1 / 15

ICS3U – String

ICS3U – String. Teacher: Mr. Ho Course URL: http://computerNHSS.wikispaces.com. What is String in Java. String is a data type It is a sequence of characters E.g.1, “Computer” E.g.2, “We love Computer Science!”. How to Use String in Java.

ferrol
Download Presentation

ICS3U – String

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

  2. What is String in Java • String is a data type • It is a sequence of characters • E.g.1, “Computer” • E.g.2, “We love Computer Science!”

  3. How to Use String in Java • First, declare a variable of String type inside the main method • In main(): String word = “Apple”;

  4. Built-in Methods • String word = “AppLe”; • word.length(); // length of the word • word.charAt(3); // character at index 3 • word.substring(2, 4); // substring • word.toUpperCase(); // upper case • word.toLowerCase(); // lower case • word.indexOf(‘p’); // first occurrence of ‘p’ • word.indexOf(“pp”); // first occurrence of “pp” • word.lastIndexOf(‘p’); // last occurrence of ‘p’ • word.equals(“apple”); // Is “AppLe” equal “apple”? • word.equalsIgnoreCase(“apple”);

  5. word.length() In the main() method: String word = “AppLe”; System.out.println(word.length()); Output: 5

  6. word.charAt(3) In the main() method: String word = “AppLe”; System.out.println(word.charAt(3)); Output: L

  7. word.substring(2, 4) In the main() method: String word = “AppLe”; System.out.println(word.substring(2,4)); Output: pL

  8. word.toUpperCase() In the main() method: String word = “AppLe”; System.out.println(word.toUpperCase()); Output: APPLE

  9. word.toLowerCase() In the main() method: String word = “AppLe”; System.out.println(word.toLowerCase()); Output: apple

  10. word.indexOf(‘p’) In the main() method: String word = “AppLe”; System.out.println(word.indexOf(‘p’)); Output: 1

  11. word.indexOf(“pp”) In the main() method: String word = “AppLe”; System.out.println(word.indexOf(“pp”)); Output: 1

  12. word.lastIndexOf(‘p’) In the main() method: String word = “AppLe”; System.out.println(word.lastIndexOf(‘p’)); Output: 2

  13. word.equals(“apple”) In the main() method: String word = “AppLe”; System.out.println(word.equals(“apple”)); Output: false

  14. word.equalsIgnoreCase(“apple”) In the main() method: String word = “AppLe”; System.out.println( word.equalsIgnoreCase(“apple”)); Output: true

  15. Classwork • Go to http://computerNHSS.wikispaces.com • Download the classwork: • JBB_pg_16.pdf • JBB_pg_17.pdf • Work on #1, 2, and 4

More Related