In this article, we will discuss different variants of index of methods to get first occurrence of character/substring using String’s indexOf() method
1. String’s indexOf() method:
- This String method is used to get 1st index of specified/passed character/substring from invoking string
- Note: There are 4 variants or overloaded indexOf() methods
1.1 Method Signature:
public int indexOf(int ch);
public int indexOf(int ch, int fromIndex);
public int indexOf(String str);
public int indexOf(String str, int fromIndex);
1.2 Parameters:
- ch –> character to be searched, to get 1st occurrence
- fromIndex –> position from where search need to start
- str –> substring to be searched, to get 1st occurrence
1.3 Returns:
indexOf() method | Returns |
indexOf(int ch) | Returns 1st occurrence of specified character |
indexOf(int ch, int fromIndex) | Returns 1st occurrence of specified character, starting from specified index |
indexOf(String str) | Returns 1st occurrence of specified substring |
indexOf(String str, int fromIndex) | Returns 1st occurrence of specified substring, starting from specified index |
2. Examples on indexOf() method:
- Sample Java program to get 1st occurrence of specified character/substring using String’s indexOf() method
StringIndexOfMethod.java
package in.bench.resources.string.methods;
public class StringIndexOfMethod {
public static void main(String[] args) {
// sample string
String str1 = "BenchResource.Net";
// to get index of char 'R'
int indexCh = str1.indexOf('R');
// printing to console
System.out.println("Index of char 'R' is : "
+ indexCh);
// to get index of char 'R',
// starting from specified position
int indexChfrom = str1.indexOf('r', 7);
// printing to console
System.out.println("Index of char 'r',"
+ " starting from 7th position is : "
+ indexChfrom);
// to get index of substring 'Resource'
int indexSubstring = str1.indexOf("Resource");
// printing to console
System.out.println("Index of substring 'Resource' is : "
+ indexSubstring);
// to get index of substring 'sour',
// starting from specified pos
int indexSubstringfrom = str1.indexOf("sour", 6);
// printing to console
System.out.println("Index of substring 'sour',"
+ " starting from 6th position is : "
+ indexSubstringfrom);
}
}
Output:
Index of char 'R' is : 5
Index of char 'r', starting from 7th position is : 10
Index of substring 'Resource' is : 5
Index of substring 'sour', starting from 6th position is : 7
Related Articles:
- Java – String charAt(int index) method
- Java – String compareTo(String anotherString) method
- Java – String compareToIgnoreCase(String str) method
- Java – String concat(String str) method
- Java – String contains(CharSequence s) method
- Java – String contentEquals(StringBuffer sb) method
- Java – String copyValueOf(char[] data) method (2)
- Java – String endsWith(String suffix) method
- Java – String equals(Object anObject) method
- Java – String equalsIgnoreCase(Object anObject) method
- Java – String format(String format, Object… args) method
- Java – String getBytes() method (4)
- Java – String getChars() method
- Java – String hashCode() method
- Java – String indexOf() method (4)
- Java – String intern() method
- Java – String isEmpty() method
- Java – String join() method (2)
- Java – String lastIndexOf() method (4)
- Java – String length() method
- Java – String matches(String regex) method
- Java – String regionMatches() method (2)
- Java – String replace(char oldChar, char newChar) method
- Java – String replace(CharSequence target, CharSequence replacement) method
- Java – String replaceAll(String regex, String replacement) method
- Java – String replaceFirst(String regex, String replacement) method
- Java – String split(String regex) method
- Java – String split(String regex, int limit) method
- Java – String startsWith(String prefix) method
- Java – String startsWith(String prefix, int toffset) method
- Java – String subSequence(int beginIndex, int endIndex) method
- Java – String substring(int beginIndex) method
- Java – String substring(int beginIndex, int endIndex) method
- Java – String toCharArray() method
- Java – String toLowerCase() method (2)
- Java – String toUpperCase() method (2)
- Java – String toString() method
- Java – String trim() method
- Java – String valueOf() method (9)
References:
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
Happy Coding !!
Happy Learning !!