AP® Computer Science A glossary

indexOf

Course: AP® Computer Science A Shows up in: Unit 1: Using Objects and Methods

The String method indexOf(String str), which returns the index of the first occurrence of str, or -1 if str is not found.

Appears in the course and exam description (CED): 1.15.B.2

Only the first occurrence counts. A result of -1 means not found, so check it before using it as an index.

String word = "INTERFACE";
System.out.println(word.indexOf("E"));
System.out.println(word.indexOf("X"));

Output:

3
-1
Learn it properly, with a checkpoint 1.15 String Manipulation

Related terms

Was this page helpful?

No names are ever shown, and every public comment is checked by us first. Please do not include personal details. To get a reply, email [email protected].

↑ Top

Enlarged view of the selected figure