πŸ“š

Β >Β 

πŸ’»Β 

Β >Β 

πŸ“±

2.7 String Methods

8 min readβ€’july 22, 2024

Avanish Gupta

Avanish Gupta

user_sophia9212

user_sophia9212


AP Computer Science AΒ πŸ’»

130Β resources
See Units

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:
Courtesy of Java.

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. Keep in mind that the equals() method is case-sensitive, meaning that β€œhi” and β€œHi” will return false.
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.

AP CS A Java Quick Reference Sheet

Worried about remembering all those String methods for the AP CS A exam? Don’t be! You get to use this AP CS A Java Quick Reference Sheet during the exam. These ⬇️ are all the methods we just went over from the reference sheet!
  • int length() method returns the number of characters in the string, including spaces and special characters like punctuation.
  • String substring(int from, int to) method returns a new string with the characters in the current string starting with the character at the from index and ending at the character before the to index (if the to index is specified, and if not specified it will contain the rest of the string).
  • int indexOf(String str) method searches for the string str in the current string and returns the index of the beginning of str in the current string or -1 if it isn’t found.
  • int compareTo(String other) returns a negative value if the current string is less than the other string alphabetically, 0 if they have the same characters in the same order, and a positive value if the current string is greater than the other string alphabetically.
  • boolean equals(String other) returns true when the characters in the current string are the same as the ones in the other string. This method is inherited from the Object class, but is overridden which means that the String class has its own version of that method.

String Method Practice Problems

indexOf Practice Problems

Reminder: The method indexOf returns the first position of the passed str in the current string starting from the left (from 0).
What is the value of pos after the following code executes?
String s6 = "hello world";
int pos = s6.indexOf("l");
A. 2
B. 1
C. 4
D. -1
Β 
Answer: A. 2
Β 
What is the value of pos after the following code executes?
String s7 = "java programming";
int pos = s7.indexOf("p");
A. 2
B. 1
C. 5
D. -1
Β 
Answer: C. 5
Β 
What is the value of pos after the following code executes?
String s8 = "computer science";
int pos = s8.indexOf("c");
A. 2
B. 0
C. 4
D. -1
Β 
Answer: B. 0
Β 
What is the value of pos after the following code executes?b
String s9 = "software development";
int pos = s9.indexOf("f");
A. 2
B. 1
C. 4
D. -1
Β 
Answer: A. 2
Β 
What is the value of pos after the following code executes?
String s10 = "artificial intelligence";
int pos = s10.indexOf("i");
A. 2
B. 3
C. 4
D. -1
Β 
Answer: B. 3

Length Practice Problems

Reminder: Length returns the number of characters in the string.
What is the value of len after the following code executes?
String s2 = "abcdef";
int len = s2.length();
A. 2
B. 3
C. 6
D. -1
Answer: C. 6
What is the value of len after the following code executes?
String s3 = "dog";
int len = s3.length();
A. 2
B. 3
C. 4
D. -1
Answer: B. 3
What is the value of len after the following code executes?
String s4 = "abc";
int len = s4.length();
A. 2
B. 3
C. 4
D. -1
Answer: B. 3
What is the value of len after the following code executes?
String s5 = "school";
int len = s5.length();
A. 2
B. 3
C. 6
D. -1
Answer: C. 6
What is the value of len after the following code executes?
String s6 = "book";
int len = s6.length();
A. 2
B. 3
C. 4
D. -1
Answer: C. 4

Substring Practice Problems

Reminder:
  • The method substring(int from, int to) returns all the characters from the starting index to the last index -1.
  • The method substring(index) will return all characters starting the index to the end of the string.
What is the value of s2 after the following code executes?
String s3 = "cat";
String s2 = s3.substring(1,3);
A. cat
B. c
C. ca
D. at
Answer: D. at
What is the value of s2 after the following code executes?
String s4 = "hello";
String s2 = s4.substring(3);
A. lo
B. o
C. el
D. ell
Answer: A. lo
What is the value of s2 after the following code executes?
String s5 = "abcdefg";
String s2 = s5.substring(2,5);
A. abcdefg
B. cde
C. def
D. c
Answer: B. cde
What is the value of s2 after the following code executes?
String s6 = "dog";
String s2 = s6.substring(1);
A. og
B. g
C. do
D. d
Answer: A. og
What is the value of s2 after the following code executes?
String s7 = "book";
String s2 = s7.substring(2,4);
A. book
B. oo
C. ok
D. bo
Answer: C. ok

compareTo Practice Problems

Reminder:Β 
For stringOne.compareTo(stringTwo)...Β 
  • If stringOne comes before stringTwo alphabetically, a negative number is returned. (negative (< 0))
  • If the strings are the same, 0 is returned.Β 
  • If stringOne comes after stringTwo alphabetically, a positive number is returned.Β  (positive (> 0))
What is the value of answer after the following code executes?
String s3 = "Banana";
String s4 = "Apple";
int answer = s3.compareTo(s4);
A. positive (> 0)
B. 0
C. negative (< 0)
Answer: A. positive (> 0)
What is the value of answer after the following code executes?
String s5 = "Blue";
String s6 = "Blue";
int answer = s5.compareTo(s6);
A. positive (> 0)
B. 0
C. negative (< 0)
Answer: B. 0
What is the value of answer after the following code executes?
String s7 = "Cat";
String s8 = "Dog";
int answer = s7.compareTo(s8);
A. positive (> 0)
B. 0
C. negative (< 0)
Answer: C. negative (< 0)
What is the value of answer after the following code executes?
String s9 = "Hello";
String s10 = "World";
int answer = s9.compareTo(s10);
A. positive (> 0)
B. 0
C. negative (< 0)
Answer: C. negative (< 0)
What is the value of answer after the following code executes?
String s11 = "Sunday";
String s12 = "Monday";
int answer = s11.compareTo(s12);
A. positive (> 0)
B. 0
C. negative (< 0)
Answer: A. positive (> 0)
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.