Skip Top Navigation Bar

Obtaining Substrings from a String Object

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.

String substring(int beginIndex)

This method returns

Your Turn

Let's try it in the Java Playground.

String substring(int beginIndex, int endIndex)

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

The method returns

Your Turn

Let's try it in the Java Playground.

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.

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.

Complete List of String Learn Tutorials

Resources

Next Learn Tutorial