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 ” “ Jenn Lawrence ” name = “ Barney Rubble ” ; “ Barney Rubble ” Strings are immutable
String methods String name = “ Barney ” ; name System.out.println(name.toUpperCase()); System.out.println(name.toLowerCase()); “ Barney ” System.out.println( “ length = “ + name.length()); 012345 System.out.println( “ first = “ + name.charAt(0)); System.out.println( “ last = “ + name.charAt(5)); System.out.println( “ last = “ + name.charAt(6)); BARNEY barney length = 6 first = B last = y 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());
Pair Practice Download and complete StringPractice.java
Recommend
More recommend