Java String indexOf() Method
-- Learning not just technology, but dreams!
- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
Java Tutorial
Java Tutorial Java Introduction Java Development Environment Setup Java Basic Syntax Java Comments Java Objects and Classes Java Basic Data Types Java Variable Types Java Variable Naming Rules Java Modifier Types Java Operators Java Loop Structures β for, while and doβ¦while Java Conditional Statements β ifβ¦else Java switch case Statement Java Number & Math Classes Java Character Class Java String Class Java StringBuffer and StringBuilder Classes Java Arrays Java Date and Time Java Regular Expressions Java Methods Java Constructors Java Stream, File and IO Java Scanner Class Java Exception Handling
Java Object-Oriented
Java Inheritance Java Override/Overload Java Polymorphism Java Abstraction Java Encapsulation Java Interfaces Java Enums Java Packages Java Reflection
Java Advanced Tutorial
Java Data Structures Java Collections Framework Java ArrayList Java LinkedList Java HashSet Java HashMap Java Iterator Java Object Java NIO Files Java Generics Java Serialization Java Networking Java Sending Email Java Multithreading Java Applet Basics Java Documentation Comments Java Examples Java 8 New Features Java MySQL Connection Java 9 New Features Java Quiz Java Common Libraries
Java StringBuffer and StringBuilder Classes
Java String indexOf() Method
The indexOf() method has the following four forms:
- public int indexOf(int ch): Returns the index of the first occurrence of the specified character in this string, or -1 if the string does not contain such a character.
- public int indexOf(int ch, int fromIndex): Returns the index of the first occurrence of the specified character in this string, starting the search at the specified index, or -1 if the string does not contain such a character.
- int indexOf(String str): Returns the index of the first occurrence of the specified substring, or -1 if the string does not contain such a substring.
- int indexOf(String str, int fromIndex): Returns the index of the first occurrence of the specified substring, starting the search at the specified index, or -1 if the string does not contain such a substring.
Syntax
public int indexOf(int ch ) or public int indexOf(int ch, int fromIndex) or int indexOf(String str) or int indexOf(String str, int fromIndex)
Parameters
- ch -- a character, Unicode encoding.
- fromIndex -- the index to start the search from. The first character is at index 0, the second at index 1, and so on.
- str -- the substring to search for.
Return Value
The index of the first occurrence of the character or substring in the string, or -1 if not found.
Example 1
public class Main{
public static void main(String args[]){
String string = "aaa456ac";
// Find the index of the specified character in the string. Returns the index if found; returns -1 if not found.
System.out.println(string.indexOf("b")); // indexOf(String str); Returns: -1, "b" does not exist
// Start searching from the fourth character position onwards, including the current position
System.out.println(string.indexOf("a",3));//indexOf(String str, int fromIndex); Returns: 6
// (Difference from above: the parameter above is of type String, the parameter below is of type int) Reference data: a-97, b-98, c-99
// Search from the beginning to see if the specified character exists
System.out.println(string.indexOf(99));//indexOf(int ch); Returns: 7
System.out.println(string.indexOf('c'));//indexOf(int ch); Returns: 7
// Search for ch from fromIndex. This is a character variable, not a string. The number corresponding to character 'a' is 97.
System.out.println(string.indexOf(97,3));//indexOf(int ch, int fromIndex); Returns: 6
System.out.println(string.indexOf('a',3));//indexOf(int ch, int fromIndex); Returns: 6
}
}
The output is:
-1
6
7
7
6
6
Example 2
public class Test{
public static void main(String args[]){
String Str = new String(":www.");
String SubStr1 = new String("");
String SubStr2 = new String("com");
System.out.print("Find the first occurrence of character 'o' :");
System.out.println(Str.indexOf('o'));
System.out.print("Find the first occurrence of character 'o' starting from position 14 :");
System.out.println(Str.indexOf('o', 14));
System.out.print("Find the first occurrence of substring SubStr1 :");
System.out.println(Str.indexOf(SubStr1));
System.out.print("Find the first occurrence of substring SubStr1 starting from position 15 :");
System.out.println(Str.indexOf(SubStr1, 15));
System.out.print("Find the first occurrence of substring SubStr2 :");
System.out.println(Str.indexOf(SubStr2));
}
}
The output of the above program is:
Find the first occurrence of character 'o' :12
Find the first occurrence of character 'o' starting from position 14 :17
Find the first occurrence of substring SubStr1 :9
Find the first occurrence of substring SubStr1 starting from position 15 :-1
Find the first occurrence of substring SubStr2 :16
YouTip