Java String indexOf() method
Types of indexOf() methods in Java
public int indexOf(String str) {}
public int indexOf(int ch) {}
public int indexOf(String str, int fromIndex) {}
public int indexOf(int ch, int fromIndex) {}
- public int indexOf(String str) {}:
Example 01 of indexOf(String str) method
public class IndexOf {
public static void main(String[] args)
{
String s = new String("Nature");
System.out.println(s.indexOf("t"));
}
}
2
here, what actually happened is 't' is at index 2 of the string "Nature" that is why it will return 2 and hence this method is called in the print line stream, it has also printed on the console.
Example 02 of indexOf(String str) method
public class IndexOf {
public static void main(String[] args)
{
String s = new String("bugaati veyron is so awesome");
System.out.println(s.indexOf("veyron"));
}
}
8
- public int indexOf(int ch) {}:
Example 01 of indexOf(int ch) method
public class IndexOf {
public static void main(String[] args)
{
String s = new String("black");
System.out.println(s.indexOf(97));
}
}
2
- public int indexOf(String str, int fromIndex) {}:
Example 01 of indexOf(String str, int fromIndex) method
public class IndexOf {
public static void main(String[] args)
{
String s = new String("black is always black");
System.out.println(s.indexOf("l",9));
}
}
10
This method indexOf(String str, int fromIndex) actually gives you the index of the string passed but there is one more parameter which is nothing but to give the index from where you want to start the search of the string whose index you want to return, for example in the above example we have written indexOf("l",9) which means to return the index of very first occurrence of 'l' in the string and start searching from the index number 9 where 'a' is there so from the character 'a' searching will be start and at the very next index 'l' is there at index number 10 hence it has returned 10.
Example 02 of indexOf(String str, int fromIndex) method
public class IndexOf {
public static void main(String[] args)
{
String s = new String("black is always black and always black");
System.out.println(s.indexOf("always",15));
}
}
26
here indexOf("always",15) will search for string "always" from the index number 15 and hence the index of second "always" in the string is returned.
- public int indexOf(int ch, int fromIndex) {}:
Example 01 of indexOf(int ch, int fromIndex) method
public class IndexOf {
public static void main(String[] args)
{
String s = new String("Hustle is Hustle");
System.out.println(s.indexOf(72,1));
}
}
10
In this example, we write indexOf(72,1) which means to search the element whose ASCII value is 72 ( we know that the ASCII value of 'H' is 72) and start the search from the index number 1 so as we move further we find that another 'H' is come at index number 10 and hence it returned 10.
Example 02 of indexOf(int ch, int fromIndex) method
public class IndexOf {
public static void main(String[] args)
{
String s = new String("elephant");
System.out.println(s.indexOf(101,1));
}
}
2
In this example, we write indexOf(101,1) which means to search the element whose ASCII value is 101 (we know that 'e' has the ASCII value 101) and start the search from index number 1 where character 'l' is there so as we move further we find that at the index number 2 'e' (ASCII value is 101) is there so it has returned 2.
Hello, visitor your comments are so helpful for us so do not hesitate just write the comment we read all your comments so don't think your comment goes waste.