<< Hide Menu

String Methods

4 min readjuly 11, 2024

Milo Chang

Milo Chang

Milo Chang

Milo Chang

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