DOC PREVIEW
FIU COP 2210 - Major String Class Methods

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Major String Class MethodsI. The length() Method (and the null String)II. SubstringsIII. The indexOf Method (a Substring Locator)IV. The substring Method (a Substring Builder)V. The toUpperCase and toLowerCase MethodsVI. The charAt MethodComputer Programming I Instructor: Greg ShawCOP 2210 Class NotesMajor String Class MethodsI. The length() Method (and the null String)This method takes no arguments and returns an int, the number ofcharacters in the string object for which the method is called.Examples: String word = “Basket” ;int len = word.length() ; // len gets 6word = word + “ball” ;len = word.length() ; // len gets 10Another example:word = “” ; // the “null” stringlen = word.length() ; // len gets 0In this example, String object variable word is initialized to thenull string (i.e., a string of no characters), so method lengthreturns 0.Note that this is not the same as:word = null ; // stores a null reference in object var wordIn this case, word contains a null reference (i.e., is known not topoint to an object. So word.length() would throw a“NullPointerException” because there is no object for which to callthe length method. (See picture on board)(Recall that Java automatically initializes object variables to null).II. SubstringsA substring is any contiguous portion of a string. For example, hereare some substrings of the string “Florida”:Flo lori or ida Florid r Florida ridThese are not substrings: flo rolF Fo Lori ORIII. The indexOf Method (a Substring Locator)Syntax: string-object.indexOf(expression)where expression is a string or char expressionReturns: “the starting position of the first occurrence ofexpression in the object for which indexOf is called” Note that the first character of a string occupies position numberzero, and not position number one. Here are some examples:String magic = "Abracadabra!" ;int pos = magic.indexOf("A") ; // pos gets 0pos = magic.indexOf("Abra") ; // 0 againpos = magic.indexOf("abra") ; // pos gets 7pos = magic.indexOf('b') ; // pos gets 1 (note char argument)pos = magic.indexOf(magic) ; // 0 (every string is a substring// of itself)String word = "ada" ;pos = magic.indexOf(word) ; // pos gets 5pos = word.indexOf(magic) ; // pos gets -1 As shown in the last example, if the argument is not asubstring of the object for which indexOf is called, -1 isreturnedThere is an overloaded version of indexOf that takes two arguments.The first is the string or character to be located and the second isan integer that specifies the position at which to begin the search.Example:pos = magic.indexOf(‘a’,4) ; // pos gets 5Here we begin the search in position 4 (the 5th character), so thefirst ‘a’ found is in position 5 (the 6th character). Note that theposition returned is always relative to the beginning of the string,no matter where we begin searching.IV. The substring Method (a Substring Builder)Syntax: string-object.substring(start, pastEnd)where start and pastEnd are integer expressions, andstart = the position of the first character of the substringpastEnd = one greater than the position of the last character Returns: “A substring of the object beginning with the character inposition start and ending with the character in positionpastEnd-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),11) ; // "adabra"Special cases:1. If start is negative, a StringIndexOutOfBoundsException isthrown.2. If start is equal to the number of characters in the stringand pastEnd is equal to start (i.e., exactly 1 greater thanthe position of the last character), the null string isreturned.3. If start is greater than the number of characters in thestring (i.e., at least 2 greater than the position of the lastcharacter), a StringIndexOutOfBoundsException is thrown.4. If pastEnd is equal to start, the null string is returned.5. If pastEnd is less than start, aStringIndexOutOfBoundsException is thrown.6. If pastEnd is greater than the number of characters in thestring (i.e., at least 2 greater than the position of the lastcharacter), a StringIndexOutOfBoundsException is thrown.There is an overloaded version of substring that takes only oneargument, the position of the first character, and returns a substringconsisting of all the characters from that position to the end of thestring.Examples: String magic = "Abracadabra!" ;String sub = magic.substring(7) ; // sub gets "abra!"sub = magic.substring(0) ; // "Abracadabra!"sub = magic.substring( magic.length()-1 ) ; // "!"V. The toUpperCase and toLowerCase MethodsSyntax: string-object.toUpperCase()Returns: “an all UPPERCASE version of string-object” I.e., all lowercase letters will be CAPITALIZED (othercharacters will not be affected)Syntax: string-object.toLowerCase()Returns: “an all lowercase version of string-object” I.e., all uppercase letters will be “decapitated” (othercharacters will not be affected)VI. The charAt MethodSyntax: string-object.charAt(index)Returns: “the character (as a char, not as a String) at positionindex in the object for which the method is called” 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 thenumber of characters in the string minus one, aStringIndexOutOfBoundsException is thrown.Note that none of the methods of the String class modify the stringobject for which they are called. String objects are immutable.That is, a String object cannot be modified. However, the objectvariable "pointing to" it can be made to point to a different


View Full Document
Download Major String Class Methods
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Major String Class Methods and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Major String Class Methods 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?