Skip to main content

Section 7.1 Strings Revisited

Throughout the first chapters of this book we have used strings to represent words or phrases that we wanted to print out. Our definition was quite direct: a string is zero or more characters inside quotes.
But there’s more we can do with strings than print them; we can manipulate strings. For example:
  • Change a name in the form "Fulano, Juan" to "Juan Fulano".
  • Given a phone number like "408-555-1212", separate out the area code, prefix, and number.
  • Take a Windows file name like "C:\Users\Phuong\Documents\report.docx" and separate out the drive letter and the file name.
  • Sort strings into dictionary order
  • Search for a word within a string (like “find” in a word processor).
  • Replace a word with another within a string (like “replace” in a word processor).
In this chapter, we’ll investigate the methods used to do these kinds of operations.