Table of Contents
indexOf method can take char or string as argument and also you can provide fromIndex(Index after which you want to use indexOf method) also.
Methods:
public int indexOf(int ch)
returns index of first occurrence of character within String
public int indexOf(int ch,int fromIndex)
returns index of first occurrence of character within String, starting from specific index “fromIndex”
public int indexOf(String)
returns index of first occurrence of substring within String
public int indexOf(int ch,int fromIndex)
returns index of first occurrence of substring within String, starting from specific index “fromIndex”
All indexOf methods return -1 if char or substring is not present in the String
String indexOf Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package org.arpit.java2blog; public class StringIndexOfExample { /* * @ Author : Arpit Mandliya */ public static void main(String args[]) { String str="Hello world from java2blog"; // Using index of method with character System.out.println("---------------------------------------------------"); System.out.println("Using indexOf method with character"); System.out.println("---------------------------------------------------"); System.out.println("Index of character b in "Hello world from java2blog" is : "+str.indexOf('b')); System.out.println(); String str1="from"; System.out.println("---------------------------------------------------"); System.out.println("Using indexOf method with fromIndex with character"); System.out.println("---------------------------------------------------"); System.out.println("Index of character o from index 6 in "Hello world from java2blog" is : "+str.indexOf('o',6)); System.out.println(); // Using index of method with String System.out.println("---------------------------------------------------"); System.out.println("Using indexOf method with String"); System.out.println("---------------------------------------------------"); System.out.println("Index of String "from" in "Hello world from java2blog" is : "+str.indexOf(str1)); System.out.println(); String howDo="How do you do"; System.out.println("---------------------------------------------------"); System.out.println("Using indexOf method with fromIndex with String"); System.out.println("---------------------------------------------------"); System.out.println("Index of String "do" from index 9 in "How do you do" is : "+howDo.indexOf("do",9)); System.out.println(); System.out.println("---------------------------------------------------"); System.out.println("If String or char is not available"); System.out.println("---------------------------------------------------"); System.out.println("Index of "from" in "How do you do" is : "+howDo.indexOf("from")); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
-------------------- Using indexOf method with character -------------------- Index of character b in "Hello world from java2blog" is : 22 -------------------- Using indexOf method with fromIndex with character -------------------- Index of character o from index 6 in "Hello world from java2blog" is : 7 -------------------- Using indexOf method with String -------------------- Index of String "from" in "Hello world from java2blog" is : 12 -------------------- Using indexOf method with fromIndex with String -------------------- Index of String "do" from index 9 in "How do you do" is : 11 -------------------- If String or char is not available -------------------- Index of "from" in "How do you do" is : -1 |