Java String indexOf(String str, int fromIndex) method with example

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 returned index is the smallest value k for which: k >= fromIndex && this.startsWith(str, k)
If no such value of k exists, then -1 is returned.

Method Signature

public int indexOf(String str,
int fromIndex)

Parameters:

str – the substring to search for.fromIndex – the index from which to start the search.

Returns:

the index of the first occurrence of the specified substring, starting at the specified index, or -1 if there is no such occurrence.

String indexOf(String str, int fromIndex) method internal implementation

 public int indexOf(String str, int fromIndex) {
        return indexOf(value, 0, value.length,
                str.value, 0, str.value.length, fromIndex);
    }

indexOf(String str, int fromIndex) method internally calls static int indexOf(char[] source, int sourceOffset, int sourceCount,char[] target, int targetOffset, int targetCount,int fromIndex).

static int indexOf(char[] source, int sourceOffset, int sourceCount,
            char[] target, int targetOffset, int targetCount,
            int fromIndex) {
        if (fromIndex >= sourceCount) {
            return (targetCount == 0 ? sourceCount : -1);
        }
        if (fromIndex < 0) {
            fromIndex = 0;
        }
        if (targetCount == 0) {
            return fromIndex;
        }

        char first = target[targetOffset];
        int max = sourceOffset + (sourceCount - targetCount);

        for (int i = sourceOffset + fromIndex; i <= max; i++) {
            /* Look for first character. */
            if (source[i] != first) {
                while (++i <= max && source[i] != first);
            }

            /* Found first character, now look at the rest of v2 */
            if (i <= max) {
                int j = i + 1;
                int end = j + targetCount - 1;
                for (int k = targetOffset + 1; j < end && source[j]
                        == target[k]; j++, k++);

                if (j == end) {
                    /* Found whole string. */
                    return i - sourceOffset;
                }
            }
        }
        return -1;
    }

Totally four variants of indexOf method there in Java String class.

  1. Java String indexOf(int ch) method with example
  2. Java String indexOf(int ch, int fromIndex) method with example
  3. Java String indexOf(String str) method with example
  4. Java String indexOf(String str, int fromIndex) method with example

Java String indexOf(String str, int fromIndex) method example

package com.ehowtonow.java.lang.string;

public class IndexOfExample {

	public static void main(String[] args) {

		String s = "ehowtonow This is java string tutorials by  ehowtonow - java tutorials";
		System.out.println("index of java : " + s.indexOf("java"));
		System.out.println("index of java in sub string : " + s.indexOf("java", 25));
		System.out.println("index of ehowtonow : " + s.indexOf("ehowtonow"));
		System.out.println("index of ehowtonow  in sub string : " + s.indexOf("ehowtonow", 5));
		System.out.println("index of tutorials : " + s.indexOf("tutorials"));
		System.out.println("index of tutorials  in sub string : " + s.indexOf("tutorials", 35));
	}

}

Output

index of java : 18
index of java in sub string : 56
index of ehowtonow : 0
index of ehowtonow in sub string : 44
index of tutorials : 30
index of tutorials in sub string : 61


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java String compareToIgnoreCase() method with example
  2. Java String codePointAt() method with example
  3. Java String indexOf(int ch, int fromIndex) method with example
  4. Java String indexOf(String str) method with example
  5. Java String codePointBefore() method with example
  6. How to find last occurrence of substring inside string
  7. String Concatenation in Java
  8. Java String concat() method with example
  9. Java String charAt() method with example
  10. How to format the String by using Locale
  11. Java String indexOf(String str, int fromIndex) method with example
  12. How to Split the String in Java
  13. How to get Character unicode point in a String
  14. Java String compareTo() method with example
  15. Java String codePointCount() method with example

Be the first to comment

Leave a Reply

Your email address will not be published.


*