Select Page
Milo is a high school senior who is very involved politically and is currently volunteering for Elizabeth Warren's campaign. He is an active leader in a variety of extracurricular activities both at his school and in his community. Milo enjoys swimming, performing community service, learning about computer science, studying history, and reading news articles.

These are common String methods that you should understand in order to do the FRQs and answer multiple-choice questions on the AP Computer Science A Exam.

In order to demonstrate these methods, we will use these Strings:

``````String a = "apple";
String b = "Bob is a high school student who likes apples";
String c = "apple";``````

## TheĀ equals(String anotherString)Ā Method ā

Strings are not Java primitives, so you cannot compare their values usingĀ ==. To compare Strings to each other, you have to use theĀ equals(String anotherString)Ā method. This method returns a boolean that tells you whether the two Strings are equal to one another.

``````a.equals("apple"); // This returns "true"
a.equals(c); // This returns "true"
b.equals(a); // This returns "false"``````

## TheĀ compareTo(String anotherString) Method š

This method compares two strings to see which one “comes first”. Ā A negative value means that the String in the front comes “before” the String in the back. A positive value means that the String in the front comes “after” the String in the back.

For the most part, the “order” that theĀ compareTo(String anotherString)Ā method uses is alphabetical order, except all of the capital letters come before the lower case letters.

If the first letter in both Strings are identical, the computer will compare the next two letters, going until the end. If all of the letters in the front are identical but one String is shorter, the shorter String comes “before” the longer one. If the two Strings are completely identical, this method will return 0.

``````a.compareTo(c); // This returns 0
b.compareTo(a); // This returns a negative number (capital B comes before a)
a.compareTo("appl"); // This returns a positive number ("appl" is shorter)``````

Tip: Remember to write out the steps as you trace code so that it’s easier to understand. TheĀ compareTo(String anotherString)Ā method can be confusing, but you can ensure that you won’t get lost by writing out what letters are being compared. Once you know which String comes “first”, you can figure out if the method returns a positive or negative value (or 0).

## TheĀ indexOf(String anotherString) Method š¤

If you want to find where a certain letter or phrase is, you use theĀ indexOf(String anotherString)Ā method. This method will return the index of where this letter or phrase first starts. (Don’t forget that spaces between words count as indexes too.)

If the letter or phrase doesn’t show up in the String, the method returns -1.

Remember: Since Strings start at index 0, the second letter is at index 1, the third letter is at index 2, and so on.

``````a.indexOf('p'); // This returns 1
b.indexOf("high school"); // This returns 9
a.indexOf("cat"); // This returns -1
b.indexOf("dent"); // This returns 24``````

Keep this in mind: Even if the phrase you check for does not appear independently in the String, it can still be detected. For example, the computer will find the index of “moth” if you run it on “smother” because the letter combination is there, even though the words have totally different meanings.

## TheĀ length()Ā Method

To find out how long a String is, you use theĀ length()Ā method, which returns an integer that tells you how long the String is.

``````a.length(); // This returns 5
b.length(); // This returns 45
c.length(); // This returns 5``````

## TheĀ substring(int start, int end)Ā Method ā

To access just one part of a String, you use the substring(int start, int end)Ā method. The first number you put in tells the computer which index to start at (the letter at this index is included). The last number you put in tells the computer which index to stop the substring at (the letter at this index is not included).

If you leave the last index empty, the substring will start at the index you give and continue to the end of the String. Make sure to know the last example; while it may seem counter-intuitive, the substring starting at the string’s length does return an empty String.

``````a.substring(2,4); // This returns "pl"
b.substring(5,24); // This returns "s a high school stu"
c.substring(1); // This returns "pple"
a.substring(a.length());// This returns an empty String.``````

Important note: These methods can be “stacked” together
You can call multiple String methods in a row, like this:

``````a.substring(0,1).equals(b.substring(7,8)); // This returns true
b.substring(5,24).compareTo(c); // This returns a positive number``````

Even though you can chain these methods together, it is best not to do this because you might make a mistake and it’s harder to read. Try to do it this way:

```String d = a.substring(0,1); Ā // d = "a" String e = b.substring(7,8); Ā // e = "a" d.equals(e) // This returns true ("a" equals "a")```

String f = b.substring(5,24); // f = “s a high school stu”
f.compareTo(c); // This returns a positive number (“s” comes after “a”)

## Understanding Concatenation š

You can’t edit a String after you set it, but you can add onto it this way:

``````String temp = ""; // temp = ""
temp += "cat"; // temp = "cat"
temp += "s"; // temp = "cats"``````

You aren’t editing StringĀ temp, you’re erasing what was inĀ tempĀ and replacing it with what used to be inĀ tempĀ and concatenating a new String. Remember thatĀ temp += “s”Ā is the same asĀ temp = temp + “s”, which is alsoĀ temp = “cat” + “s”. The value of temp (which wasĀ “cat”) is replaced byĀ “cat” + “s”, orĀ “cats”.

## Summary: šāØš

• Know theĀ equals(String anotherString)Ā method
• Know theĀ compareTo(String anotherString)Ā method
• Know theĀ indexOf(String anotherString)Ā method
• Know theĀ length()Ā method
• Know theĀ substring(int start, int end)Ā method
• Know that you can concatenate Strings