πŸ“š

Β >Β 

πŸ’»Β 

Β >Β 

✏️

String Methods

4 min readβ€’july 11, 2024

Milo Chang

Milo Chang


AP Computer Science AΒ πŸ’»

130Β resources
See Units

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
Browse Study Guides By Unit
βž•Unit 1 – Primitive Types
πŸ“±Unit 2 – Using Objects
πŸ–₯Unit 3 – Boolean Expressions & if Statements
πŸ•ΉUnit 4 – Iteration
βš™οΈUnit 5 – Writing Classes
⌚️Unit 6 – Array
πŸ’ΎUnit 7 – ArrayList
πŸ’»Unit 8 – 2D Array
πŸ–²Unit 9 – Inheritance
πŸ–±Unit 10 – Recursion
🧐Exam Skills

Fiveable
Fiveable
Home
Stay Connected

Β© 2024 Fiveable Inc. All rights reserved.


Β© 2024 Fiveable Inc. All rights reserved.