The Java String indexOf(int ch) method returns the index within this string of the first occurrence of the specified character.
If a character with value ch occurs in the character sequence represented by this String object, then the index(Unicode code units) of the first such occurrence is returned.
DeclarationFollowing is the declaration for Java String indexOf() method
public int indexOf(int ch)Parameters
ch − This is a character (Unicode code point).
Return ValueThis method returns the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.
ExceptionNA
String indexOf(int ch, int fromIndex) MethodThe Java String indexOf(int ch, int fromIndex) method returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
If a character with value ch occurs in the character sequence represented by this String object at an index no smaller than fromIndex, then the index of the first such occurrence is returned.
DeclarationFollowing is the declaration for Java String indexOf() method
public int indexOf(int ch, int fromIndex)Parameters
ch − This is a character (Unicode code point).
fromIndex − This is the index to start the search from.
This method returns the index of the first occurrence of the character in the character sequence represented by this object that is greater than or equal to fromIndex, or -1 if the character does not occur.
ExceptionNA
String indexOf(String str) MethodThe Java String indexOf(String str) method returns the index within this string of the first occurrence of the specified substring. The integer returned is the smallest value k such that: this.startsWith(str, k) is true.
DeclarationFollowing is the declaration for Java String indexOf() method
public int indexOf(String str)Parameters
str − This is value of a string.
Return ValueThis method returns if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.
ExceptionNA
String indexOf(String str, int fromIndex) MethodThe Java String indexOf(String str, int fromIndex) method returns the index within this string of the first occurrence of the specified substring, starting at the specified index. The integer returned is the smallest value k for which −
k > = Math.min(fromIndex, this.length()) && this.startsWith(str, k)
If no such value of k exists, then -1 is returned.
DeclarationFollowing is the declaration for Java String indexOf() method
public int indexOf(String str, int fromIndex)Parameters
str − This is the substring for which to search.
fromIndex − This is the index from which to start the search.
This method returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
ExceptionNA
Example - Getting Index of a Char from a StringThe following example shows the usage of Java String indexOf(char) method. We created a string literal with the value "This is tutorialspoint". Then using the indexOf(char) method, we are retrieving the index Of e.
package com.tutorialspoint; public class StringDemo { public static void main(String[] args) { String str = "This is tutorialspoint"; // returns the index of occurrence of character s System.out.println("index of letter 's' = " + str.indexOf('s')); // returns -1 as character e is not in the string System.out.println("index of letter 'e' = " + str.indexOf('e')); } }
Let us compile and run the above program, this will produce the following result −
index of letter 's' = 3 index of letter 'e' = -1Example - Getting Index of a char from a String with From Index
The following example shows the usage of Java String indexOf(char, int) method. We created a string literal with the value "This is tutorialspoint". Then using the indexOf(char, int) method, we are retrieving the index Of e.
package com.tutorialspoint; public class StringDemo { public static void main(String[] args) { String str = "This is tutorialspoint"; // returns positive value as character is located System.out.println("index of letter 't' = " + str.indexOf('t', 14)); // returns positive value as character is located System.out.println("index of letter 's' = " + str.indexOf('s', 10)); // returns -1 as character is not in the string System.out.println("index of letter 'e' = " + str.indexOf('e', 5)); } }
Let us compile and run the above program, this will produce the following result −
index of letter 't' = 21 index of letter 's' = 16 index of letter 'e' = -1Example - Getting Index of a String from a String
The following example shows the usage of Java String indexOf(string) method. We created a string literal with the value "Collections of tutorials at tutorials point". Then using the indexOf(string) method, we are retrieving the index Of tutorials and admin.
package com.tutorialspoint; public class StringDemo { public static void main(String[] args) { String str1 = "Collections of tutorials at tutorials point"; // returns index of first character of the substring "tutorials" System.out.println("index = " + str1.indexOf("tutorials")); // returns -1 as substring "admin" is not located System.out.println("index = " + str1.indexOf("admin")); } }
Let us compile and run the above program, this will produce the following result −
index = 15 index = -1Example - Getting Index of a String from a String with FromIndex
The following example shows the usage of Java String indexOf(string, int) method. We created a string literal with the value "Collections of tutorials at tutorials point". Then using the indexOf(string, int) method, we are retrieving the index Of tutorials and admin.
package com.tutorialspoint; public class StringDemo { public static void main(String[] args) { String str1 = "Collections of tutorials at tutorials point"; /* search starts from index 10 and if located it returns the index of the first character of the substring "tutorials" */ System.out.println("index = " + str1.indexOf("tutorials", 10)); /* search starts from index 9 and returns -1 as substring "admin" is not located */ System.out.println("index = " + str1.indexOf("admin", 9)); } }
Let us compile and run the above program, this will produce the following result −
index = 15 index = -1
java_lang_string.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4