1 / 12

Strings

Strings. An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE. String Accessors (What does this mean?). message = "Welcome"; message.length() (gives us what?). Retrieving Characters in a String.

Download Presentation

Strings

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. Strings • An extension of types • A class that encompasses a character array and provides many useful behaviors • Chapter 9 • Strings are IMMUTABLE

  2. String Accessors (What does this mean?) message = "Welcome"; message.length() (gives us what?)

  3. Retrieving Characters in a String • Cannot use message[0](strings are immutable) • Use message.charAt(index) • Index starts from 0 Is charAt() a public or private method? • Can you write a loop for a string to print out each char?

  4. String Comparisons String s1 = new String("Welcome“); String s2 = "Welcome"; if (s1.equals(s2)){ // do s1 and s2 have the same contents? } if (s1 == s2) { // do s1 and s2 have the same reference? }

  5. String Comparisons, cont. • compareTo(Object object) String s1 = new String("Welcome“); String s2 = "hello"; if (s1.compareTo(s2) > 0) { // is s1 greater than s2? } else if (s1.compareTo(s2) == 0) { // do s1 and s2 have the same contents? } else { // s1 is lexicographically less than s2 }

  6. Strings are Immutable! • Immutable means we can’t change them. • Can’t do: String str = “hello”; str[3] = ‘b’; • So then why does this work? String str = "Hello"; System.out.println(str); //Prints Hello str = "Help!"; System.out.println(str); //Prints Help!

  7. Why can I do this?(Or, isn’t this mutating the string?) String str = "Mississippi"; System.out.println(str); //Prints Mississippi str = str.replace("i", "!"); System.out.println(str); //Prints M!ss!ss!pp!

  8. String methods: charAt(int index) – gets the character at the index within the string and returns the char indexOf(char c)- sees if a character is within a string and returns an int representing the index indexOf(char c, intfromindex) - sees if a character is within a string from fromindex onwards length() – returns number of characters in a string (note that for strings this is a method. What is it for arrays?) substring(intbeginindex) – extracts and returns a substring from a string, starting at beginindex and going to the end of the string substring(intbeginindex, intendindex) – extracts and returns a substring from beginindex to endindex toLowerCase() – returns a string that is the lower case version of the string toUpperCase() – returns a string that is the upper case version of the string trim() - removes white space (space char) from both ends of a string and returns that string toCharArray() – converts string to character array and returns that array equalsIgnoreCase(String otherstring) – compares string with otherstring and returns Boolean value (true if equals, false if doesn’t) compareToIgnoreCase(String otherstring) – compares string with otherstring, and returns int (1 if string is “greater”, 0 if equal, and -1 if “less than”.

  9. toString() method • Printing objects: • If you print an object that doesn’t have a toString() method, you print out the hash representation of the object • Not what we want. • All java built-in objects have a toString method implemented. • E.g., Color x = new Color(255,0,0); //Color is a built-in java class -RGBSystem.out.println(x.toString()); • Will print out: java.awt.Color[r=255,g=0,b=0] (this is the string that the toString method explicitly created and returned.) System.out.println(x)

  10. toString() • The println() method can take an Object as an argument.  • This version will implicitly call toString() for you and print the result. • Meaning, you can do: Color x = new Color( 255, 0, 0 );System.out.println( x ); • Also happens with concatenation, e.g.,: Color x = new Color( 255, 0, 0 );String str = “This is color: “; str += x; • toString() is a method that you should write when you create a class definition. • will automatically be used as above.

  11. Wanna Try? (Quickly write toString()) public class StudentInfo { private String first; private String last; private String[] schedule; public StudentInfo(String f, String l, String[] sched) { first = f; last=l; schedule = sched; } //… }

  12. My toString() public String toString() { String str = ""; str += first + "\t"+ last + "\n"; for (inti = 0; i < schedule.length; i++) { str+= schedule[i] "\t"; } return(str); } Note the \t and \n These are known as “escape sequences” • \t adds a tab • \n adds a new line (makes the printout go to the next line.

More Related