String class methods help build algorithms for solving common programming problems and are commonly tested in the Free Response Questions (FRQs) on the AP exam. They simplify tasks like extracting specific parts of text, cleaning data, and looping through characters.
The following string methods from Unit 2.7 are most commonly used for these algorithms.
- length(): Returns the number of characters in a string, including spaces.
- substring(start, end): Extracts part of the string from start (inclusive) to end (exclusive).
- charAt(index): Retrieves the character at a specified position (index) in the string.
- indexOf(char): Finds the position of the first occurrence of a character and returns -1 if not found.
- toLowerCase() and toUpperCase(): Converts all letters in the string to lowercase or uppercase, respectively.
- trim(): Removes leading and trailing whitespace from a string.