.substring( )

The .substring() method is an essential tool in Java, especially for students preparing for the AP Computer Science A exam. This method allows you to extract parts of a string based on specified indexes, making it helpful for manipulating text data. In this post, we’ll explore how .substring() works, with examples tailored to what you need to know for AP CS A.

What is .substring()?

In Java, the .substring() method is used to create a new string from an existing one by specifying the starting and (optional) ending indexes.

The syntax for .substring() is as follows:

string.substring(int beginIndex);
string.substring(int beginIndex, int endIndex);

beginIndex: The starting position (inclusive) for the substring.

endIndex (optional): The ending position (exclusive) for the substring.

Note: Java uses zero-based indexing, so the first character in a string is at index 0.

Using .substring() with One Parameter

When you provide only one parameter, .substring(beginIndex), the method returns a substring from the specified start index to the end of the string.

Example 1:

String text = "Hello, World!";

// Starts at index 7 and goes to the end
String result = text.substring(7); 

// Output: "World!"
System.out.println(result);  

In this example, .substring(7) extracts the substring starting from index 7 up to the end of the string.

Using .substring() with Two Parameters

When you provide both beginIndex and endIndex, .substring(beginIndex, endIndex) extracts a substring from the start index to just before the end index.

Example 2:

String text = "Hello, World!";

// Extracts from index 0 up to (but not including) index 5
String result = text.substring(0, 5);  

// Output: "Hello"
System.out.println(result);

In this example, .substring(0, 5) captures the first five characters of the string, returning “Hello”.

Extracting Characters in a Loop

Combining .substring() with loops allows you to extract portions of a string repeatedly or examine individual characters. This approach is useful for tasks like creating subsections or analyzing specific parts of a string in AP CS A.

Example 3:

String text = "Programming";

for (int i = 0; i < text.length(); i++) {
    // Extracts one character at a time
    String letter = text.substring(i, i + 1);  
    System.out.println(letter);
}

Output:
P
r
o
g
r
a
m
m
i
n
g

Here, .substring(i, i + 1) extracts each character individually as the loop iterates through the entire string.

Moving Window Technique

The “moving window” technique is helpful for analyzing portions of text in a sliding window of fixed size. This technique is commonly applied in algorithms and text analysis.

Example 4:

String text = "ABCDEF";

int windowSize = 2;

for (int i = 0; i <= text.length() - windowSize; i++) {
    String window = text.substring(i, i + windowSize);
    System.out.println(window);
}

Output:
AB
BC
CD
DE
EF

In this example, .substring(i, i + windowSize) moves one character at a time, extracting overlapping substrings of length windowSize.

Important Points to Remember

1. Inclusive Start, Exclusive End: .substring(beginIndex, endIndex) includes beginIndex but excludes endIndex. This is crucial when you want precise substring results.

2. Index Out of Bounds: If you provide indexes that fall outside the string length, Java will throw a StringIndexOutOfBoundsException. Ensure that beginIndex and endIndex are within the valid range.

3. Edge Cases: If beginIndex and endIndex are equal, .substring() will return an empty string.

Example 5:

In the similar context, if endIndex is just beginIndex + 1, then it returns single characters at a time.

Recap

The .substring() method is essential in Java for extracting and analyzing parts of a string. Here’s a quick recap:

One Parameter: .substring(beginIndex) extracts from beginIndex to the end.

Two Parameters: .substring(beginIndex, endIndex) extracts from beginIndex to just before endIndex.

In AP CS A, .substring() combined with loops allows you to:

• Extract individual characters or sections

• Break down text into smaller parts

• Apply the moving window technique

Leave a comment