String Methods

Sophie Anderson
7 min read
Listen to this study note
Study Guide Overview
This AP Computer Science A study guide covers string manipulation in Java, including using external libraries and APIs. It explores the String class and its methods like length(), substring(), equals(), and compareTo(). It also provides practice problems and examples for indexOf(). The guide references the AP CS A Java Quick Reference Sheet and includes multiple choice and free response practice questions.
#AP Computer Science A: String Manipulation Study Guide 🚀
Hey there, future AP Computer Science A rockstar! This guide is designed to be your ultimate resource for mastering String manipulation, especially as you gear up for the big exam. Let's dive in and make sure you're feeling confident and ready to ace it!
#Introduction to External Libraries and APIs
#What are External Libraries and APIs?
- Java's base installation is powerful, but external libraries and APIs expand its capabilities.
- These resources provide pre-written code for common tasks, saving you time and effort.
- Think of them as toolboxes filled with specialized tools for your coding projects. 🧰
#Java Documentation
- Every Java class, library, and API comes with documentation.
- Documentation lists available methods and how to use them.
- Check out the official Java documentation for the String class here: String Class Documentation
#The String Class
-
The String class is part of the
java.lang
package. -
Classes in
java.lang
are automatically imported, so no need to import them manually. 🎉
#String Methods: Accessing Substrings
#Key Concepts
- Substring: A sequence of characters within a larger string.
- Character: A substring with a length of 1. - Zero-Indexed Language: Java starts counting at 0, not 1. 🤯
#length()
Method
int length()
: Returns the number of characters in the string.
java String str = "Hello"; int len = str.length(); // len will be 5 ```
#substring()
Method
String substring(int beginIndex, int endIndex)
: Returns a substring frombeginIndex
up to (but not including)endIndex
.String substring(int beginIndex)
: Returns a substring frombeginIndex
to the end of the string.
java String str = "Peter Cao"; String sub1 = str.substring(0, 5); // sub1 will be "Peter" String sub2 = str.substring(6); // sub2 will be "Cao" ```
#Accessing the nth Character
-
Use
str.substring(n-1, n)
to get the nth character. Remember zero-indexing!
#String Methods: Equality and Comparison
#equals()
Method
boolean equals(String other)
: Checks if two strings have the same characters in the same order.- Returns
true
if strings are equal,false
otherwise. - Case-sensitive: "hi" is not equal to "Hi".
java String str1 = "hello"; String str2 = "hello"; String str3 = "Hello"; boolean isEqual1 = str1.equals(str2); // true boolean isEqual2 = str1.equals(str3); // false ```
#compareTo()
Method
int compareTo(String other)
: Compares two strings lexicographically (alphabetical order).- Returns:
- A negative value if the current string comes before the
other
string. - 0 if the strings are equal.
- A positive value if the current string comes after the
other
string.
- A negative value if the current string comes before the
java String str1 = "apple"; String str2 = "banana"; int result = str1.compareTo(str2); // result will be negative ```
#AP CS A Java Quick Reference Sheet
- You can use the AP CS A Java Quick Reference Sheet during the exam!
- It includes all the String methods we've discussed:
int length()
String substring(int from, int to)
String substring(int from)
int indexOf(String str)
int compareTo(String other)
boolean equals(String other)
#String Method Practice Problems
#indexOf
Practice Problems
- Reminder:
indexOf()
returns the first position of the passed string in the current string, starting from the left (index 0).
Problem 1:
java
String s6 = "hello world";
int pos = s6.indexOf("l");
A. 2 B. 1 C. 4 D. -1
Answer: A. 2
Problem 2:
java
String s7 = "java programming";
int pos = s7.indexOf("p");
A. 2 B. 1 C. 5 D. -1
Answer: C. 5
Problem 3:
java
String s8 = "computer science";
int pos = s8.indexOf("c");
A. 2 B. 0 C. 4 D. -1
Answer: B. 0
Problem 4:
java
String s9 = "software development";
int pos = s9.indexOf("f");
A. 2 B. 1 C. 4 D. -1
Answer: A. 2
Problem 5:
java
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.
Problem 1:
java
String s2 = "abcdef";
int len = s2.length();
A. 2 B. 3 C. 6 D. -1
Answer: C. 6
Problem 2:
java
String s3 = "dog";
int len = s3.length();
A. 2 B. 3 C. 4 D. -1
Answer: B. 3
Problem 3:
java
String s4 = "abc";
int len = s4.length();
A. 2 B. 3 C. 4 D. -1
Answer: B. 3
Problem 4:
java
String s5 = "school";
int len = s5.length();
A. 2 B. 3 C. 6 D. -1
Answer: C. 6
Problem 5:
java
String s6 = "book";
int len = s6.length();
A. 2 B. 3 C. 4 D. -1
Answer: C. 4
#substring
Practice Problems
- Reminder:
substring(int from, int to)
returns characters from the starting index up to (but not including) the ending index.substring(index)
returns characters from the index to the end of the string.
Problem 1:
java
String s3 = "cat";
String s2 = s3.substring(1,3);
A. cat B. c C. ca D. at
Answer: D. at
Problem 2:
java
String s4 = "hello";
String s2 = s4.substring(3);
A. lo B. o C. el D. ell
Answer: A. lo
Problem 3:
java
String s5 = "abcdefg";
String s2 = s5.substring(2,5);
A. abcdefg B. cde C. def D. c
Answer: B. cde
Problem 4:
java
String s6 = "dog";
String s2 = s6.substring(1);
A. og B. g C. do D. d
Answer: A. og
Problem 5:
java
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 beforestringTwo
alphabetically, a negative number is returned. - If the strings are the same, 0 is returned.
- If
stringOne
comes afterstringTwo
alphabetically, a positive number is returned.
- If
Problem 1:
java
String s3 = "Banana";
String s4 = "Apple";
int answer = s3.compareTo(s4);
A. positive (> 0) B. 0 C. negative (< 0)
Answer: A. positive (> 0)
Problem 2:
java
String s5 = "Blue";
String s6 = "Blue";
int answer = s5.compareTo(s6);
A. positive (> 0) B. 0 C. negative (< 0)
Answer: B. 0
Problem 3:
java
String s7 = "Cat";
String s8 = "Dog";
int answer = s7.compareTo(s8);
A. positive (> 0) B. 0 C. negative (< 0)
Answer: C. negative (< 0)
Problem 4:
java
String s9 = "Hello";
String s10 = "World";
int answer = s9.compareTo(s10);
A. positive (> 0) B. 0 C. negative (< 0)
Answer: C. negative (< 0)
Problem 5:
java
String s11 = "Sunday";
String s12 = "Monday";
int answer = s11.compareTo(s12);
A. positive (> 0) B. 0 C. negative (< 0)
Answer: A. positive (> 0)
Practice Question
#Multiple Choice Questions
MCQ 1:
java
String str = "Coding";
String sub = str.substring(1, 4);
System.out.println(sub);
What is the output of the code?
A) Cod
B) odi
C) odin
D) ding
Correct Answer: B) odi
MCQ 2:
java
String s1 = "apple";
String s2 = "Apple";
if (s1.equals(s2)) {
System.out.println("Same");
} else {
System.out.println("Different");
}
What is the output of the code?
A) Same
B) Different
Correct Answer: B) Different
MCQ 3:
java
String text = "programming";
int index = text.indexOf("m");
System.out.println(index);
What is the output of the code?
A) 2 B) 3 C) 7 D) -1
Correct Answer: C) 7
#Free Response Question
FRQ:
Write a method stringManipulator
that takes a String as a parameter and returns a new String based on the following rules:
- If the String length is less than 3, return the original String.
- If the String length is greater or equal to 3, return a new String that consists of the first character, the last character, and the middle character of the original String. If the String length is even, use the character at the index
length()/2 -1
as the middle character.
For example:
stringManipulator("ab")
should return"ab"
stringManipulator("abc")
should return"acb"
stringManipulator("abcd")
should return"adc"
stringManipulator("abcdef"
Explore more resources

How are we doing?
Give us your feedback and let us know how we can improve