CSCI 144 - Introduction to Computer Science Instructor: John - - PowerPoint PPT Presentation
CSCI 144 - Introduction to Computer Science Instructor: John - - PowerPoint PPT Presentation
1 CSCI 144 - Introduction to Computer Science Instructor: John Goettsche Computer Science Department Pacific Lutheran University Spring 2018 Strings Strings name String name; ? name = Fred Flintstone ; Fred Flintstone
Strings
Strings
String name; name ? name = “Fred Flintstone”;
“Jenn Lawrence”
Strings are immutable
“Fred Flintstone”
name = “Barney Rubble”;
“Barney Rubble”
String methods
String name = “Barney”;
name
“Barney”
System.out.println(“length = “ + name.length()); length = 6 System.out.println(name.toUpperCase()); System.out.println(name.toLowerCase()); BARNEY barney
012345
System.out.println(“first = “ + name.charAt(0)); System.out.println(“last = “ + name.charAt(5)); first = B last = y System.out.println(“last = “ + name.charAt(6)); Exception in thread “main” java.lang.StringIndexOutOfBoundsException
String Practice – What’s the output?
String greeting = “Hello World!”; System.out.println(greeting.toUpperCase()); System.out.println(greeting.length()); System.out.println(greeting.charAt(9)); System.out.println(greeting.charAt(greeting.length());