Back to Iteration

Lesson PreviewLesson 3 of 5 in this unit

4.3 Developing Algorithms Using Strings

Part of Iteration in AP® Computer Science A. Read the opening section, then create a free account for the full interactive lesson, practice, and progress tracking.

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.

Continue the full lesson in Stellar

Create a free account to unlock the remaining sections, checkpoints, course practice, and progress tracking for AP® Computer Science A.