03StringsIOAndMethods

03StringsIOAndMethods - CSE 114 Computer Science I Strings...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 114 – Computer Science I Strings, I/O, and Methods Bonneville Salt Flats, Utah
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Java character data char Type for a single character Each char is a 2-byte number (Unicode) char symbol = '7'; System.out.println(symbol); System.out.println((int)symbol); Unicode character set ‘0’ (48) … ‘9’ (57) ‘A’ (65) … ‘Z’ (90) ‘a’ (97) … ‘z’ (122) Output? 7 55
Background image of page 2
String A class in Java API http://java.sun.com/javase/6/docs/api/java/lang/String.html Used for text: String s1 = "Rudie Can't"; String s2 = s1 + " Fail"; System.out.println(s2); Output? Rudie Can’t Fail
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
More about Strings Each character is stored at an index String sentence = "Charlie Don't Surf"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 C h a r l i e D o n ' t S u r f The String class (from J2SE) has methods to process strings. System.out.println("charAt(6) is " + sentence.charAt(6)); System.out.println(sentence.toUpperCase()); System.out.println(sentence); System.out.println(sentence.substring(0,7) + sentence.substring(14)); charAt(6) is e CHARLIE DON'T SURF Charlie Don't Surf CharlieSurf Output to Console window by println methods
Background image of page 4
Strings are immutable! There are no methods to change them once they have been created So how can I change a String? 1. make a new one 2. assign the new one to the old variable String word = "Hello"; word.substring(0,4); System.out.println(word); word = word.substring(0, 4); System.out.println(word); Output? Hello Hell
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
String functions “+” used for building new Strings. Ex: String s = "If Music "; s = s + "Could Talk"; int mins = 4, secs = 36; String t = s + " (" + mins + ":" + secs + ")"; System.out.println(t); Output? If Music Could Talk (4:36)
Background image of page 6
Useful String functions charAt equals equalsIgnoreCase compareTo startsWith endsWith indexOf lastIndexOf replace substring toLowerCase toUpperCase trim s.equals(t) returns true if s and t have same letters false otherwise
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Special Characters '\n' – newline '\t' – tab '\"' – quotation mark Ex, how can we print <img src="./pic.jpg" /> String s = "<img src=\"./pic.jpg\" />"; System.out.println(s);
Background image of page 8
How can we get user input? API methods
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/13/2010 for the course CSE 114 taught by Professor Tashbook during the Spring '08 term at SUNY Stony Brook.

Page1 / 32

03StringsIOAndMethods - CSE 114 Computer Science I Strings...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online