Lecture 20 (Sherriff) - String Methods

Lecture Date: Friday, February 27

We’ll take a few minutes to talk about the test and how regrades work. A key for the test will be available in Collab Resources.

We’ll then get into methods, specifically looking at some methods in the String class that you’ll need for your first partner projects.

You should start to get used to looking at APIs - application programming interfaces. Basically, the information you can look at to determine how to use a particular class in your program.

The String API is available online.

For example, one method you will probably want to use is indexOf(String str) (described here in the String API. Assume we are searching through a string called stringInfo.

If you want to find a particular character, stringInfo.indexOf("@"); returns the first index where a @ appears. If you then do stringInfo.indexOf("@", token+1); where token is the location of the first @, then this will return the location of the second @'

Other String methods you probably will want to look at:

  • lastIndexOf, like indexOf but looks before instead of after the starting point

  • substring, useful for pulling out just one bit of a string

  • replace, which you used in POTD 11, useful for e.g., replacing at with @.

What makes up a method? What are the different parts of the line that defines a method? Can we write our own methods to process Strings? We’ll discuss all of that today in class.

Parseltongue.java