the string class trace code
play

The String Class Trace Code Constructing a String String s = - PDF document

The String Class Trace Code Constructing a String String s = "Java"; String message2 = new String("Welcome to Java); s = "HTML"; String s1 = new String(); Since strings are used frequently, Java provides a


  1. The String Class Trace Code  Constructing a String String s = "Java"; String message2 = new String("Welcome to Java“); s = "HTML"; String s1 = new String();  Since strings are used frequently, Java provides a shorthand initializer for creating a string: After executing String s = "Java"; After executing s = "HTML"; s String message1 = "Welcome to Java“; : String s : String This string object is now unreferenced String s2 = “”; String object for "Java" String object for "Java" : String Contents cannot be changed String object for "HTML" 1 4 Trace Code Chapter 8 Strings and Text I/O String s = "Java"; s = "HTML"; After executing String s = "Java"; After executing s = "HTML"; s : String s : String This string object is now unreferenced String object for "Java" String object for "Java" Contents cannot be changed : String String object for "HTML" 2 5 Strings Are Immutable String Comparisons  A String object is immutable; its contents cannot be java.lang.String changed +equals(s1: String): boolean Returns true if this string is equal to string s1.  Does the following code change the contents of the +equalsIgnoreCase(s1: String): Returns true if this string is equal to string s1 case- boolean insensitive. string? +compareTo(s1: String): int Returns an integer greater than 0, equal to 0, or less than 0 to indicate whether this string is greater than, equal to, or String s = "Java"; less than s1. s = "HTML"; +compareToIgnoreCase(s1: String): Same as compareTo except that the comparison is case- int insensitive. +regionMatches(toffset: int, s1: String, Returns true if the specified subregion of this string exactly offset: int, len: int): boolean matches the specified subregion in string s1. +regionMatches(ignoreCase: boolean, Same as the preceding method except that you can specify toffset: int, s1: String, offset: int, whether the match is case-sensitive. len: int): boolean +startsWith(prefix: String): boolean Returns true if this string starts with the specified prefix. +endsWith(suffix: String): boolean Returns true if this string ends with the specified suffix. 3 6

  2. String Comparisons Finding String Length equals Finding string length using the length() method: String s1 = new String("Welcome“); String s2 = new String(“welcome”); message = "Welcome"; message.length() if (s1.equals(s2)){ // s1 and s2 have the same contents } if (s1 == s2) { // s1 and s2 have the same reference } 7 10 String Comparisons, cont. Retrieving Individual Characters in a String compareTo  Use message.charAt(index)  Index starts from 0 String s1 = new String("Welcome“); String s2 = "welcome"; Indices 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 if (s1.compareTo(s2) > 0) { message W e l c o m e t o J a v a // s1 is greater than s2 } message.charAt(0) message.length() is 15 message.charAt(14) else if (s1.compareTo(s2) == 0) { // s1 and s2 have the same contents } Do not use message[0] !!! else // s1 is less than s2 8 11 String Length, Characters, and Combining String Concatenation Strings String s3 = s1.concat(s2); String s3 = s1 + s2; java.lang.String +length(): int Returns the number of characters in this string. +charAt(index: int): char Returns the character at the specified index from this string. +concat(s1: String): String Returns a new string that concatenate this string with string s1. s1 + s2 + s3 + s4 + s5 (((s1.concat(s2)).concat(s3)).concat(s4)).concat(s5); 9 12

  3. Extracting Substrings Examples “welcome” "Welcome".toLowerCase() java.lang.String "Welcome".toUpperCase() “WELCOME” Returns this string’s substring that begins with the character at the +subString(beginIndex: int): specified beginIndex and extends to the end of the string, as String “WAlcomA” shown in Figure 8.6. "Welcome".replace('e', 'A’) +subString(beginIndex: int, Returns this string’s substring that begins at the specified endIndex: int): String beginIndex and extends to the character at index endIndex – 1, as shown in Figure 8.6. Note that the character at endIndex is not part of the substring. 13 16 Splitting a String Extracting Substrings  Using the substring method in the String class String str = “Cat-Dog-Mouse”; String s1 = "Welcome to Java"; String[] tokens = str.split(“-”); Indices 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 System.out.println(tokens[2]); message W e l c o m e t o J a v a System.out.println(“eats”); System.out.println(tokens[0]); message.substring(0, 11) message.substring(11) String s2 = s1.substring(0, 11) + "HTML"; {“Cat”, “Dog”, “Mouse”} 14 17 Finding a Character or a Substring in a Converting, Replacing, and Splitting Strings String java.lang.String java.lang.String +indexOf(ch: char): int Returns the index of the first occurrence of ch in the string. Returns -1 if not matched. +toLowerCase(): String Returns a new string with all characters converted to lowercase. +indexOf(ch: char, fromIndex: Returns the index of the first occurrence of ch after fromIndex in +toUpperCase(): String Returns a new string with all characters converted to uppercase. int): int the string. Returns -1 if not matched. +trim(): String Returns a new string with blank characters trimmed on both sides. +indexOf(s: String): int Returns the index of the first occurrence of string s in this string. Returns -1 if not matched. +replace(oldChar: char, Returns a new string that replaces all matching character in this +indexOf(s: String, fromIndex: Returns the index of the first occurrence of string s in this string newChar: char): String string with the new character. int): int after fromIndex. Returns -1 if not matched. +replaceFirst(oldString: String, Returns a new string that replaces the first matching substring in +lastIndexOf(ch: int): int Returns the index of the last occurrence of ch in the string. newString: String): String this string with the new substring. Returns -1 if not matched. +replaceAll(oldString: String, Returns a new string that replace all matching substrings in this +lastIndexOf(ch: int, Returns the index of the last occurrence of ch before fromIndex newString: String): String string with the new substring. fromIndex: int): int in this string. Returns -1 if not matched. +split(delimiter: String): Returns an array of strings consisting of the substrings split by the +lastIndexOf(s: String): int Returns the index of the last occurrence of string s. Returns -1 if String[] delimiter. not matched. +lastIndexOf(s: String, Returns the index of the last occurrence of string s before fromIndex: int): int fromIndex. Returns -1 if not matched. 15 18

  4. Finding a Character or a Substring in a String Problem: Counting Each Letter in a String  This example gives a program that counts the number of occurrence of each letter in a string. "Welcome to Java".indexOf('W') returns 0.  Assume the letters are not case-sensitive. "Welcome to Java".indexOf('x') returns -1 . "Welcome to Java".indexOf('o', 5) returns 9.  CountEachLetter.java "Welcome to Java".indexOf("come") returns 3. "Welcome to Java".indexOf("Java", 5) returns 11. "Welcome to Java".indexOf("java", 5) returns -1. "Welcome to Java".lastIndexOf('a') returns 14. 19 22 Problem: Finding Palindromes Review  Objective: Checking whether a string is a palindrome: a  String class – an immutable sequence of characters string that reads the same forward and backward.  Character class – a character  CheckPalindrome.java 20 The Character Class Review questions  Suppose s is a string with the value "java". What will be assigned to x if you execute the following code? java.lang.Character +Character(value: char) Constructs a character object with char value char x = s.charAt(4); +charValue(): char Returns the char value from this object +compareTo(anotherCharacter: Character): int Compares this character with another +equals(anotherCharacter: Character): boolean Returns true if this character equals to another A. 'a' +isDigit(ch: char): boolean Returns true if the specified character is a digit B. 'v' +isLetter(ch: char): boolean Returns true if the specified character is a letter +isLetterOrDigit(ch: char): boolean C. Nothing will be assigned to x, because the execution causes Returns true if the character is a letter or a digit +isLowerCase(ch: char): boolean the runtime error StringIndexOutofBoundsException. Returns true if the character is a lowercase letter +isUpperCase(ch: char): boolean Returns true if the character is an uppercase letter +toLowerCase(ch: char): char Returns the lowercase of the specified character +toUpperCase(ch: char): char Returns the uppercase of the specified character 21

Recommend


More recommend