Explore indexOf() for Substring Position
In the previous step, we learned how to check if a string contains a substring using contains()
. Now, let's explore another useful method, indexOf()
, which not only tells us if a substring exists but also gives us its starting position within the string.
The indexOf()
method in Java's String
class returns the index within the string of the first occurrence of the specified substring. The index is the position of the first character of the substring. Remember that in programming, indexing usually starts from 0. If the substring is not found, indexOf()
returns -1.
Let's modify our previous Java file, StringContains.java
, to use the indexOf()
method.
-
Open the StringContains.java
file in the WebIDE editor.
-
Replace the entire contents of the file with the following code:
public class StringContains {
public static void main(String[] args) {
String sentence = "Java programming is fun and powerful.";
String keyword = "programming";
// Find the index of the keyword
int index = sentence.indexOf(keyword);
System.out.println("Sentence: \"" + sentence + "\"");
System.out.println("Keyword: \"" + keyword + "\"");
if (index != -1) {
System.out.println("The keyword \"" + keyword + "\" was found at index: " + index);
} else {
System.out.println("The keyword \"" + keyword + "\" was not found.");
}
String anotherKeyword = "Python";
int anotherIndex = sentence.indexOf(anotherKeyword);
System.out.println("\nAnother keyword: \"" + anotherKeyword + "\"");
if (anotherIndex != -1) {
System.out.println("The keyword \"" + anotherKeyword + "\" was found at index: " + anotherIndex);
} else {
System.out.println("The keyword \"" + anotherKeyword + "\" was not found.");
}
}
}
In this updated code:
- We use
sentence.indexOf(keyword)
to find the starting index of the keyword
within the sentence
. The result is stored in an int
variable index
.
- We use an
if
statement to check if the returned index
is -1. If it's not -1, it means the keyword was found, and we print its index. Otherwise, we print a message indicating that the keyword was not found.
- We repeat the process for "Python" to demonstrate the case where the substring is not present.
-
Save the file (Ctrl+S or Cmd+S).
-
Open the Terminal at the bottom of the WebIDE.
-
Make sure you are in the ~/project
directory.
-
Compile the modified Java program:
javac StringContains.java
-
Run the compiled Java program:
java StringContains
You should see output similar to this:
Sentence: "Java programming is fun and powerful."
Keyword: "programming"
The keyword "programming" was found at index: 5
Another keyword: "Python"
The keyword "Python" was not found.
The output shows that "programming" starts at index 5 in the sentence (remembering that 'J' is at index 0, 'a' is at index 1, and so on, including the space). It also correctly indicates that "Python" was not found.
Using indexOf()
is a powerful way to not only check for the presence of a substring but also to determine its exact location, which can be useful for various string manipulation tasks.