ics3u – string

15
ICS3U – String ICS3U – String Teacher: Mr. Ho Course URL: http://computerNHSS.wikispaces.com

Upload: ferrol

Post on 15-Jan-2016

66 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ICS3U – String

ICS3U – StringICS3U – StringTeacher: Mr. HoCourse URL: http://computerNHSS.wikispaces.com

Page 2: ICS3U – String

What is String in JavaWhat is String in JavaString is a data typeIt is a sequence of characters E.g.1, “Computer”E.g.2, “We love Computer

Science!”

Page 3: ICS3U – String

How to Use String in JavaHow to Use String in JavaFirst, declare a variable of String

type inside the main methodIn main():

String word = “Apple”;

Page 4: ICS3U – String

Built-in MethodsBuilt-in MethodsString word = “AppLe”;word.length(); // length of the wordword.charAt(3); // character at index 3word.substring(2, 4); // substringword.toUpperCase(); // upper caseword.toLowerCase(); // lower caseword.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”);

Page 5: ICS3U – String

word.length()word.length()

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

Output:5

Page 6: ICS3U – String

word.charAt(3)word.charAt(3)

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

Output:L

Page 7: ICS3U – String

word.substring(2, 4)word.substring(2, 4)

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

Output:pL

Page 8: ICS3U – String

word.toUpperCase()word.toUpperCase()

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

Output:APPLE

Page 9: ICS3U – String

word.toLowerCase()word.toLowerCase()

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

Output:apple

Page 10: ICS3U – String

word.indexOf(‘p’)word.indexOf(‘p’)

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

Output:1

Page 11: ICS3U – String

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

Output:1

Page 12: ICS3U – String

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

Output:2

Page 13: ICS3U – String

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

Output:false

Page 14: ICS3U – String

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

Output:true

Page 15: ICS3U – String

ClassworkClassworkGo to

http://computerNHSS.wikispaces.com

Download the classwork: ◦JBB_pg_16.pdf◦JBB_pg_17.pdf

Work on #1, 2, and 4