Skip Top Navigation Bar

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.

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.

int compareTo(String anotherString)

This method returns

int compareToIgnoreCase(String anotherString)

This method is similar to the compareTo method, but doesn't distinguish between uppercase and lowercase letters.

This method returns

boolean endsWith(String suffix)

This method returns

boolean equals(Object anObject)

This method returns

boolean equalsIgnoreCase(String anotherString)

This method is similar to the equals method, but doesn't distinguish between uppercase and lowercase letters.

This method returns

int indexOf(String str)

This method returns

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.

String substring(int beginIndex)

This method returns

String substring(int beginIndex, int endIndex)

This method is similar to the substring method that takes only one parameters.

The method returns

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.

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