The AP Computer Science A 5-hour live stream review is here!Β πŸ’»

Join us on May 5, 2021 for the 🌢️ AP Computer Science A Cram Finale for a last minute review to get all your questions answered!

πŸ“š

All Subjects

Β >Β 

πŸ’»Β 

AP Comp Sci A

Β >Β 

πŸ“±

Unit 2

2.7 String Methods

3 min readβ€’september 17, 2020

Peter Cao


Intro to External Libraries and APIs

Although there is a lot you can do with the factory version of Java, which is the version you get that comes right with the Java installation, there is a lot more to Java as well. You can import external libraries and APIs (application program interfaces) from the Java installation that aren't pre-loaded into your IDE or from the internet for use in your programs. These will significantly improve the functionalities of the programs that you can make.
All Java classes, libraries, and APIs come with documentation, which lists the methods of the class and also how to use the class and its methods. Here is the official Java documentation for the String class:

About the String Class

The String class is a class in the java.lang package that comes when you install Java on your computer. For your convenience, any classes from the java.lang package does not need to be imported to the program as we did with the Scanner class back in Unit 1. You can see the documentation for the String class above!

String Methods: Accessing Substrings

With strings, we can access substrings and characters in the string. A substring is a string that is included within a larger string, while a character is a substring with a length of 1. First, we need to figure out the length of a string, which is the number of characters in the string. To do this, we use the following method:
  • int length(String str)
This returns the length of the string. With this number, we can finally access substrings! To use this, we use this method:
  • String substring(int beginIndex, int endIndex)
This method returns the substring starting from the beginning index and ending just before the ending index. By convention, you may think that the first letter will have index 1, the second letter will have index 2, and so on, but this is not the case. Java is a zero-indexed language, which means that the first character has index 0, the second has index 1, and so on such that the last has index length() - 1. For example in the string String str = "Peter Cao", here are the characters and their indices:

Letters and Indices

IndexCharacter
0P
1e
2t
3e
4r
5
6C
7a
8o
Although the total length of the string is 9, notice how the last character, "o", is at index 8. To get the first word as a substring with that string, I would do:
str.substring(0,5);
This is because in "Peter", the "P" is at index 0 and the "r" is at index 4, which is before the space, which is index 5.
The substring() method has also been overloaded as well. With the overloaded method, we can return the remainder of the string with this version of the method:
  • String substring(startIndex)
With the same string we have been working on, to get the last word, we use str.substring(6) to get "Cao" because the C is at index 6.
To get the nth character, we can use substring() method as follows: str.substring(n-1, 1) because we have to remember that the nth character has index n-1 and we stop before the next index.

String Methods: Equality and Comparison

Sometimes, we want to see whether two strings are equal or see which string is alphabetically first. We use two methods to do this:
  • boolean equals(String other)
  • int compareTo(String other)
The equals() method is pretty self explanatory and you call it as follows:
stringOne.equals(stringTwo)
The method will return true if the two strings are equal and false if the two strings are not.
Other times, we want to check to see if a string comes before or after another string alphabetically. This is done using the compareTo() method as follows:
stringOne.compareTo(stringTwo)
Keep in mind that what is returned is relative to stringTwo. If stringOne comes before stringTwo alphabetically, a negative number is returned. If the strings are the same, 0 is returned. Finally, if stringOne comes after stringTwo alphabetically, a positive number is returned. This will be useful to sort multiple strings later.

Was this guide helpful?

πŸ’ͺ🏽 Are you ready for the Comp Sci exam?
Take this quiz for a progress check on what you’ve learned this year and get a personalized study plan to grab that 5!
START QUIZ
Hours Logo
Studying with Hours = the ultimate focus mode
Start a free study session
FREE AP comp sci a Survival Pack + Cram Chart PDF
Sign up now for instant access to 2 amazing downloads to help you get a 5
Browse Study Guides By Unit
πŸ™
Exam Reviews
πŸ–±
Unit 10: Recursion
βž•
Unit 1: Primitive Types
πŸ–₯
Unit 3: Boolean Expressions and if Statements
πŸ•Ή
Unit 4: Iteration
βš™οΈ
Unit 5: Writing Classes
⌚️
Unit 6: Array
πŸ’Ύ
Unit 7: ArrayList
πŸ’»
Unit 8: 2D Array
πŸ–²
Unit 9: Inheritance
Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now
πŸ“± Stressed or struggling and need to talk to someone?
Talk to a trained counselor for free. It's 100% anonymous.
Text FIVEABLE to 741741
Β© 2021 Fiveable, Inc.