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 and in Formatting Output with Escape Sequences and Text Blocks.
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.
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
.
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.
boolean endsWith(String suffix)
This method returns
true
if this String
ends with suffix
and false
otherwise.
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
.
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.
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
.
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
.
- 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
.
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.
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.
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()
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.
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