Using the String Class
In the Declaring Strings tutorial, we used String objects, created by using string literals. Reminder that a string literal is characters enclosed in double quotes (").
In Introduction to Arithmetic Expressions, we used the + operator to concatenate, or add, string literals.
We also used string literals as part of output in Introduction to Console Input and Output, Formatting Output with Escape Sequences and Formatting Output with Text Blocks.
Need a refresher on creating objects and calling methods?
Learn: Creating Objects and Calling Methods
String Constructor
While in previous String tutorials, we used string literals to create String objects. There is a String constructor that can also be used. To satisfy any curiousity, here is an example of how to create a String object using a String constructor:
String myName = new String("Duke");
We will likely stick to using string literals to create String objects.
Strings Are Immutable
Recall that the value of a String is immuntable, it cannot be changed. A String variable's value can be altered by assigning it to a new String, but not by changing the existing String. Consider the example below.
String phrase = "Duke";
phrase = phrase + " loves Java";
The value "Duke" is concatenated with " loves Java" and a new String object is created that is "Duke loves Java".
String Indexing
Each character of a String literal has an associated index, or label, with the first character being index 0 and each subsequent index is one more than the last. Consider the following example.
- The index of
D is 0.
- The index of
u is 1.
- The index of
k is 2.
- The index of
e is 3.
String Methods
We've compiled some helpful String methods below.
 |
Methods that are part of the AP Computer Science A Java subset are denoted by the APCSA label. |
A complete list of String methods can be found in the String API.
char charAt(int index)
This method returns the char value at the specified index.
Your Turn
Let's try it in the Java Playground.
- add a statement to use
charAt to access the last character of phrase and assign it to a variable named last.
start is assigned D since that is the character at index 0.
middle is assigned v since that is the 8th character in phrase or the character at index 7.
int compareTo(String anotherString)
This method returns
- 0 if this
String has the same value as anotherString.
- a value less than 0 if this
String is lexicographically less than anotherString. Lexicographically is also referred to as dictionary order. It is the way we alphabetize words. In computers each character is assigned a number based on the ASCII codes. In this system, uppercase letters have a lower value than uppercase letters. In other words, uppercase letters come before lowercase letters in alphabetical order. For example, "apple" is lexicographically before "axel", since "p" comes before "x".
- a value greater than 0 if this
String is lexicographically greater than anotherString.
It is helpful to think of lexicographical order a little like alphabetical order, but uppercase letters come before lowercase letters. Look at the two strings that are being compared, draw a < above the compareTo method if the first word comes before the second word and a > if the first word comes after the second. If it is less than, the return value is negative. If it is greater than, the return value is positive. This visualization helps to illustrate this process.
Your Turn
Let's try it in the Java Playground.
- Add a
String with the same value as name and compare the two.
value is assigned a value greater than 0, since D comes after A.
value2 is assigned a value less than 0, since A comes before D.
int compareToIgnoreCase(String anotherString)
This method is similar to the compareTo method, but doesn't distinguish between uppercase and lowercase letters.
This method returns
- 0 if this
String has the same value as anotherString.
- a value less than 0 if this
String is lexicographically less than anotherString, while ignoring the difference between upper and lowercase letters.
- a value greater than 0 if this
String is lexicographically greater than anotherString, while ignoring the difference between upper and lowercase letters.
Your Turn
Let's try it in the Java Playground.
- Predict the value of
value.
- Run the code to determine whether your prediction is correct.
boolean endsWith(String suffix)
This method returns
true if this String ends with suffix and false otherwise.
Your Turn
Let's try it in the Java Playground.
- Predict the values of
value and value2.
- Run the code to determine whether your predictions are correct.
value is assigned true since Awesomeness ends with ness.
value2 is assigned false. While Awesomeness contains some, it doesn't end with some.
boolean equals(Object anObject)
This method returns
true if the value of this String is the same as the String value of anObject. NOTE: All classes extend the Object class, meaning a String is an Object.
false if the value of this String is not the same as theString value of anObject.
Your Turn
Let's try it in the Java Playground.
- Predict the value of
value1 and value2.
- Run the code to determine whether your predictions are correct.
value1 is assigned the value false since one of the words starts with an uppercase letter and the other starts with a lowercase letter.
value2 is assigned the value true since they are the same word with all matching letters in the same case.
boolean equalsIgnoreCase(String anotherString)
This method is similar to the equals method, but doesn't distinguish between uppercase and lowercase letters.
This method returns
true if the value of this String is the same as the value of anotherString, while ignoring the difference between upper and lowercase letters.
false if the value of this String is not the same as the Value of anotherString, while ignoring the difference between upper and lowercase letters.
Your Turn
Let's try it in the Java Playground.
- Predict the values for
value1 and value2
value1 is assigned the value true since the difference in uppper and lowercase letters to start the words is ignored.
value2 is assigned the value true since they are the same word with all matching letters in the same case.
int indexOf(String str)
This method returns
- the index within this string of the first occurrence of the parameter
str.
- -1 if this word does not contain an occurrence of
str.
Your Turn
Let's try it in the Java Playground.
- Predict the values of
index1, index2, and index3.
- Run the code to see if your predictions are correct.
index1 is assigned the value 7. Starting with A being at index 0, the n in ness is at index 7.
index2 is assigned the value 3. Starting with A being at index 0, the s in some is at index 3.
index3 is assigned teh value -1. This method distinguishes between upper and lowercase letters, so Awesome and awesome are considered different values. As such, awesome does not appear in word.
There are multiple indexOf methods that take different parameters, some specifying the range of indexes where str is being searched.
int length()
This method returns the number of characters that are in the String.
Your Turn
Let's try it in the Java Playground.
- Predict the value of
wordLength.
- Add a statement to compute the length of
phrase and assign it to wordLength.
- Predict the new value of
wordLength.
- Run the code to check you predictions.
- wordLength is assigned the value 4, since there are 4 characters in
Duke.
- phraseLength is assigned the value 15, since there are 13 letters and two spaces in
phrase.
String substring(int beginIndex)
This method returns
- a part of this
String starting at beginIndex and going to the end of this String.
Your Turn
Let's try it in the Java Playground.
- Predict the value of
part.
- Run the code to determine if your prediction is correct.
part is assigned the value loves Java, since the 5th index is the l.
String substring(int beginIndex, int endIndex)
This method is similar to the substring method that takes only one parameters.
The method returns
- a part of this
String starting at beginIndex and ending at the index before endIndex. You can think of this it as beginIndex is the first value to include and endIndex is the first index not to include.
Your Turn
Let's try it in the Java Playground.
- Predict the value of
part.
- Run the code to determine if your prediction is correct.
part is assigned the value loves, since the 5th index is the l. The 10th index is the space before the J. This is the first index to not be included in the substring.
The value of endIndex can be the length of the string. Since the substring that is being created starts at beginIndex and the first index that is not included is endIndex, our call does not go out of bounds of the string.
Your Turn
Let's try it in the Java Playground.
- Predict the value of
part.
- Run the code to determine if your prediction is correct.
part is assigned the value loves Java, since the 5th index is the l. The 15th index is only one beyond the last index value. This is the first index to not be included in the substring.
A strategy that can be used to determine the substring between two indices is to draw a vertical line to the left of each index as illustrated below. The substring will be between these lines.
String toLowerCase() & String toUpperCase()
String toLowerCase()
This method creates a new String where all uppercase letters of this String have been converted to lowercase letters.
String toUpperCase()
Similar to the method toLowerCase, but the method creates a new String where all lowercase letters of this String have been converted to uppercase letters.
Your Turn
Let's try it in the Java Playground.
- Predict the values of
lower and upper.
- Run the code to determine whether your prediction is correct.
lower has been assigned the value duke loves java.
upper has been assigned the value DUKE LOVES JAVA.
split method
The split method is a String method that is part of the AP CSA Java Subset. This method returns a collection of data called an array. This method will be revisited once arrays have been introduced.
Resources
Next Learn Tutorial