This book is now obsolete Please use CSAwesome instead.
4.3. String Methods on the Exam¶
A string holds characters in a sequence. Each character is at a position or index which starts with 0 as shown below. An index is a number associated with a position in a string. The length of a string is the number of characters in it including any spaces or special characters. The string below has a length of 14.
Note
The first character in a string is at index 0 and the last characters is at the length - 1.
For the AP CS A exam there are only a few things that you have to know about strings. All of the following are included in the quick reference that you get during the exam so you don’t have to memorize these. I recommend printing a copy of the quick reference and using it when you practice for the exam. You can get it at https://secure-media.collegeboard.org/digitalServices/pdf/ap/explore-ap/AP_Computer-Science-A-Quick-Reference.pdf.
the
int length()
method returns the number of characters in the string, including spaces and special characters like punctuation.the
String substring(int from, int to)
method returns a string with the characters in the current string starting with the character at thefrom
index and ending at the character before theto
index (if theto
index is specified, and if not specified it will contain the rest of the string).the
int indexOf(String str)
method returns the index of the beginning ofstr
in the current string or -1 if it isn’t found.the
int compareTo(String other)
returns a negative value if the current string is less than theother
string, 0 if they have the same characters in the same order, and a positive value if the current string is greater than theother
string.the
boolean equals(String other)
returns true when the characters in the current string are the same as the ones in theother
string. This method is inherited from the Object class, but is overriden which means that the String class has its own version of that method.
Run the code below to see the output from length
, substring
, and indexOf
.
Note
Did you notice that message1.substring(0,3)
includes all the characters from position 0 to 2 and doesn’t include the character at position 3?
Check your understanding
- 2
- The first character is at index 0 in a string.
- 1
- The method indexOf returns the first position of the passed str in the current string starting from the left (from 0).
- 4
- Does indexOf start from the left or right?
- -1
- Does the string contain a b?
4-2-2: What is the value of pos after the following code executes?
String s1 = "abccba";
int pos = s1.indexOf("b");
- 2
- Length returns the number of characters in the string, not the number of characters in the name of the string.
- 3
- The position of the last character is 3, but the length is 4.
- 4
- Length returns the number of characters in the string.
- -1
- Length is never negative.
4-2-3: What is the value of len after the following code executes?
String s1 = "baby";
int len = s1.length();
- baby
- This would be true if substring returned all the characters from the first index to the last inclusive, but it does not include the character at the last index.
- b
- This would be true if it was s1.substring(0,1)
- ba
- This would be true if it was s1.substring(0,2)
- bab
- Substring returns all the characters from the starting index to the last index - 1.
4-2-4: What is the value of str2 after the following code executes?
String s1 = "baby";
String s2 = s1.substring(0,3);
- 7
- Count spaces and punctuation in the length.
- 8
- Did you forget to count a space or punctuation?
- 9
- The length method returns the number of characters including spaces and punctuation.
4-2-5: What is the value of len after the following executes?
String s1 = "Miss you!";
int len = s1.length();
- by
- The method substring(index) will return all characters starting the index to the end of the string.
- aby
- This would be true if it was substring(1);
- a
- This would be true if it was substring(1,2);
- b
- This would be true if it was substring(2,3);
- ba
- This would be ture if it was substring(0,2);
4-2-6: What is the value of str2 after the following code executes?
String s1 = "baby";
String s2 = s1.substring(2);
Run the example below to see the output from compareTo
and equals
.
There are lots of other methods in the String class. See the Java documentation for the String class at http://docs.oracle.com/javase/6/docs/api/java/lang/String.html. You don’t have to know all of these for the exam, but you can use them if you want to on the exam.
Note
Strings are immutable which means that they can’t change. Anything that you do to modify a string (like creating a substring or appending strings) returns a new string.
Check your understanding
- hi th
- The substring method returns the string starting at the first index and not including the last index. The method indexOf returns the index of the first place the string occurs.
- hi the
- This would be correct if substring returned all characters between the first index and last index, but does it?
- hi ther
- This would be correct if indexOf returned the last position the string str was found in the current string, does it?
- hi there
- This would be correct if indexOf returned the last position the string str was found in the current string and if substring included all characters between the start and end index. Check both of these.
4-2-8: What is the value of s2 after the following code executes?
String s1 = new String("hi there");
int pos = s1.indexOf("e");
String s2 = s1.substring(0,pos);
- Hi
- Strings are immutable, meaning they don't change. Any method that changes a string returns a new string. So s1 never changes.
- hi
- This would be true if the question was what is the value of s2 and it was substring(0,2) not (0,1)
- H
- This would be true if the question was what is the value of s2, not s1.
- h
- This would be true if the question was what is the value of s3, not s1.
4-2-9: What is the value of s1 after the following code executes?
String s1 = "Hi";
String s2 = s1.substring(0,1);
String s3 = s2.toLowerCase();
- Hi
- Is this the value of s3? What does toLowerCase do?
- hi
- How does substring work? Does it include the character at the end index?
- H
- What does toLowerCase do?
- h
- s2 is set to just "H" and s3 is set to changing all characters in s2 to lower case.
4-2-10: What is the value of s3 after the following code executes?
String s1 = "Hi";
String s2 = s1.substring(0,1);
String s3 = s2.toLowerCase();
- positive (> 0)
- H is after B in the alphabet so s1 is greater than s2.
- 0
- The method compareTo will only return 0 if the strings have the same characters in the same order.
- negative (< 0)
- This would be true if it was s2.compareTo(s1)
4-2-11: What is the value of s3 after the following code executes?
String s1 = "Hi";
String s2 = "Bye";
int answer = s1.compareTo(s2);