Java is a simple programming language that may be used in a variety of applications. The String indexof() function is commonly used in Java. It mainly used in get substring in String and in index of character.
Explanation
The indexOf() function returns the location in a string of the first occurrence of the provided character(s).
The lastIndexOf function returns the position of the final occurrence of a string’s supplied character(s).
There are 4 indexOf() methods in Java:
- public int indexOf(String str)
- public int indexOf(String str, int fromIndex)
- public int indexOf(int char)
- public int indexOf(int char, int fromIndex
Parameter:
- Str – A String value that represents the string to be searched for fromIndex.
- fromindex – An int integer representing the index position from which to begin the search from
- Char-An int value that represents a single character, such as ‘A’, or a Unicode value
Technical Details:
Return value:
- An int value reflecting the index of the character’s first occurrence in the string, or -1 if it never occurs.
Behaviour:
- The indexof() method searches the supplied Substring beginning at the beginning of the string.
- When the substring is found, it returns the index of the substring’s first character.
- When the substring cannot be found, it returns -1.
- When “fromIndex” is used, it searches before or after that index. It enables numerous occurrences of the Substring.
Method Signature:
int indexOf(String str) int indexOf(String str, int fromIndex)
Four methods Signature of IndexOf():
| int indexOf(int ch) | For the specified char value, it returns the index position. |
| int indexOf(int ch, int fromIndex) | It returns the index position from index for the specified char value. |
| int indexOf(String substring) | For the specified substring, it returns the index position. |
| int indexOf(String substring, int fromIndex) | For the specified substring, it returns the index position, and from index |
Internal of Indexof():
public int indexOf(int ch) {
return indexOf(ch, 0);
}
Example 1:
public class TechVidvan IndexOf{
public static void main(String args[]){
String s1="Indexof method is used ";
int index1=s1.indexOf("is");
int index2=s1.indexOf("index");
System.out.println(index1+" "+index2);
int index3 = s1.indexOf("is", 4);
System.out.println(index3);
int index4 = s1.indexOf('s');
System.out.println(index4);
}
}
Output:
8 0
9
3
Example 2:
public class IndexOf {
public static void main(String[] args) {
String s1 = "Java is easy to learn";
int index = s1.indexOf("easy"); System.out.println("The index of substring "+index);
}
}
Output:
The index of Substring 8
Example 3:
public class TechVidvanIndexOf {
public static void main(String[] args) {
String s1 = "This is indexOf method";
int index = s1.indexOf("method", 10);
System.out.println("index of substring "+index);
index = s1.indexOf("method", 20);
System.out.println("index of substring "+index);
}
}
Output:
index of substring 16
index of substring -1
Table:
| IndexOf(Char, Int32) | This function returns the zero-based index of the first instance of the provided Unicode character in this string. The search begins at a given character point. |
| IndexOf(String) | The zero-based index of the first occurrence of the provided string in this instance is reported. |
| IndexOf(Char) | This function returns the zero-based index of the first instance of the provided Unicode character in this string. |
| IndexOf(String, Int32) | The zero-based index of the first occurrence of the provided string in this instance is reported. The search begins at a given character point. |
Variants of indexof():
- int indexOf()
- int indexOf(char ch, int strt)
- int indexOf(String str)
- int indexOf(String str, int strt)
1. int indexOf():
This function returns the index of the first occurrence of the provided character within this string, or -1 if the character does not occur.
Syntax:
int indexOf(char ch )
Parameters:
ch : character.
Example:
public class IndexOf{
public static void main(String[] args) {
String s1 = "We use Strings";
// Passing char and index from
int index = s1.indexOf('e', 5); //Returns the index of this char
System.out.println("index of char "+index);
}
}
Output:
index of char 5
2. int indexOf(char ch, int strt)
This function returns the index of the first occurrence of the supplied character within this string, commencing the search at the specified index, or -1 if the character does not occur.
Syntax:
int indexOf(char ch, int strt)
Parameters:
ch :a character. str : the index to start the search from.
3. int indexOf(String str)
This function returns the index of the first occurrence of the provided substring within this string. If it does not appear as a substring, it returns -1.
Syntax:
int indexOf(String str)
Parameters:
str : a string.
4. int indexOf(String str, int strt)
This function returns the index of the first occurrence of the provided substring within this text. If it does not appear as a substring, it returns -1.
Syntax:
int indexOf(String str, int str)
Parameters:
str : a string.
Usage of string indexof():
- Find all occurrence-It finds all the occurrences in the Substring in the string
- Searching of substring-It mainly searches the first occurrence of Substring
- Specify a specific position- It finds the specific position of the substring in the string
- Case insensitive – It acts insensitively in finding uppercase and lowercase in string
Exceptions of Java String indexof():
- Argument- The value of the NullException is null.
- The count or startIndex of ArgumentOutOfRangeException is negative.
- StartIndex exceeds the length of this string.
- Count exceeds the length of this string less startIndex.
Advantages of String indexof() in Java:
- It aids in determining the position of Substring within the string.
- It returns to the place of the first occurrence of the substring.
- It is a standard method in programming languages that is utilized on a variety of platforms.
Disadvantages of String indexof() in Java:
- It is case-sensitive and will not find substrings in uppercase or lowercase.
- It does not return all occurrences in the substring, but only the first occurrence.
- It does not handle overlapping substring matches.
Conclusion
In Java, we learned about the String indexof() method function. It is primarily useful in the basic substring. This type of String function is used in many areas. We have learned all the content of String indexof().

