Computer Programming II COP 3337 Instructor: Greg Shaw Class Notes Major String Class Methods I. The String Concatenation Operator (+) Concatenation means joining strings together to form a new string. The string concatenation operator (the plus sign) is used to concatenate two strings. Example: String word1 = “to” , word2 = “get” , word3 = “her” ; String word4 = word1 + word2 + word3 ; // “together” If only one of the operands is a String, then Java will convert the other(s) to Strings and concatenate them, as shown here: word4 = “Answer is: ” + 3 + 7 ; word4 = “Answer is: ” + (3 + 7) ; // word4 is “Answer is 37” // word4 is “Answer is 10” II. The length() Method (and the null String) This method takes no arguments and returns an int, the number of characters in the string object for which the method is called. Examples: String word = “Basket” ; int len = word.length() ; // len gets 6 word = word + “ball” ; len = word.length() ; // len gets 10 Another example: word = “” ; len = word.length() ; // the “null” string // len gets 0 In this example, String object variable word is initialized to the null string (i.e., a string of no characters), so method length returns 0. Note that this is not the same as: word = null ; // stores a null reference in object var word In this case, word contains a null reference (i.e., is known not to point to an object. So word.length() would throw a “NullPointerException” because there is no object for which to call the length method. (See picture on board) (Recall that Java automatically initializes object variables to null). III. Substrings A substring is any contiguous portion of a string. are some substrings of the string “Florida”: Flo lori or ida Florid These are not substrings: flo r Florida rolF Fo For example, here rid Lori OR IV. The indexOf Method (a Substring Locator) Syntax: string-object.indexOf(expression) where expression is a string or character expression Returns: “the starting position of the first occurrence of expression in the object receiving the message.” Note that the first character of a string occupies position number zero, and not position number one. Here are some examples: String magic = "Abracadabra!" ; int pos = magic.indexOf("A") ; // pos gets 0 pos = magic.indexOf("Abra") ; // 0 again pos = magic.indexOf("abra") ; // pos gets 7 pos = magic.indexOf('b') ; // pos gets 1 (note char argument) pos = magic.indexOf(magic) ; // 0 (every string is a substring String word = "ada" ; pos = magic.indexOf(word) ; // pos gets 5 pos = word.indexOf(magic) ; // pos gets -1 As shown in the last example, if the argument substring of the string object, then -1 is returned is not a There is an overloaded version of indexOf that takes two arguments. The first is the string or character to be located and the second is an integer that specifies the position at which to begin the search. Example: pos = magic.indexOf(‘a’,4) ; // pos gets 5 Here we begin the search in position 4 (the 5th character), so the first ‘a’ found is in position 5 (the 6th character). Note that the position returned is always relative to the beginning of the string, no matter where we begin searching. V. The substring Method (a Substring Builder) Syntax: string-object.substring(start, pastEnd) where start and pastEnd are integer expressions, and start = the position of the first character of the substring pastEnd = one greater than the position of the last character Returns: “A substring of the object beginning with the character in position start and ending with the character in position pastEnd-1.” Examples: String magic = "Abracadabra!" ; String sub = magic.substring(0,4) ; // sub gets "Abra" sub = magic.substring(0,1) ; // "A" sub = magic.substring(4,7) ; // "cad" sub = magic.substring(0,magic.length()) ; // "Abracadabra!" String word = "ada" ; sub = magic.substring(magic.indexOf(word),12) ; // "adabra!" Special cases: 1. If start thrown. is negative, a StringIndexOutOfBoundsException 2. If start is equal to the number of characters in the string (i.e., exactly 1 greater than the position of the last character), the null string is returned. 3. If start is greater than the number of characters in the string (i.e., at least 2 greater than the position of the last character), a StringIndexOutOfBoundsException is thrown. 4. If pastEnd is equal to start, the null string is returned. 5. If pastEnd is less than StringIndexOutOfBoundsException is thrown. 6. If pastEnd is greater than the number of characters in the string (i.e., at least 2 greater than the position of the last character), a StringIndexOutOfBoundsException is thrown. start, is a There is an overloaded version of substring that takes only one argument, the position of the first character, and returns a substring consisting of all the characters from that position to the end of the string. Examples: String magic = "Abracadabra!" ; String sub = magic.substring(7) ; // sub gets "abra!" sub = magic.substring(0) ; // "Abracadabra!" sub = magic.substring( magic.length()-1 ) ; // "!" VI. The toUpperCase and toLowerCase Methods Syntax: string-object.toUpperCase() Returns: “an all UPPERCASE version of the object RTM” I.e., all lowercase letters will characters will not be affected) Syntax: be CAPITALIZED (other string-object.toLowerCase() Returns: “an all lowercase version of the object RTM” I.e., all uppercase letters will be “decapitated” (other characters will not be affected) VII. The charAt Method Syntax: string-object.charAt(index) Returns: “the character (as a char, not as a String) at position index in the object RTM” String magic = "Abracadabra!" ; char letter = magic.charAt(0) ; // letter gets 'A' letter = magic.charAt( magic.length() - 1 ) ; // '!' letter = magic.charAt( magic.length() ) ; // exception! If the index expression is less than 0, or greater than the number of characters in the string minus one, a StringIndexOutOfBoundsException is thrown. Note that none of the methods of the String class modify the string object for which they are called. String objects are immutable. That is, a String object cannot be modified. However, the String object variable "pointing to" it can be made to point to a different object. VIII. The parseInt and parseDouble Methods Syntax: Integer.parseInt(expression) where expression is a string expression Returns: an int version of expression Syntax: Double.parseDouble(expression) where expression is a string expression Returns: an double version of expression If expression does not resemble a valid integer or double number, respectively, a NumberFormatException will be thrown. Note that these methods are not String class methods – parseInt belongs to the Integer class and parsedouble to the Double class. Integer and Double are “wrapper” classes that are used when we want an object reference to “point to” a primitive int or double value. We will see how and why this is done at a later date. We can tell by the way these methods are called – using the class name – that they are static methods. As we will soon see, in Java the most useful input operations return strings. parseInt and parseDouble are commonly used to convert input strings to numbers.