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

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. For values of ch in the range from 0 to 0xFFFF (inclusive), this is the smallest value k such that: (this.charAt(k) == ch) && (k >= fromIndex)
is true. For other values of ch, it is the smallest value k such that: (this.codePointAt(k) == ch) && (k >= fromIndex)
is true. In either case, if no such character occurs in this string at or after position fromIndex, then -1 is returned. There is no restriction on the value of fromIndex. If it is negative, it has the same effect as if it were zero: this entire string may be searched. If it is greater than the length of this string, it has the same effect as if it were equal to the length of this string: -1 is returned. All indices are specified in char values (Unicode code units).

Method Signature

public int indexOf(int ch,
int fromIndex)

Parameters:

ch – a character (Unicode code point).

fromIndex – the index to start the search from.

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.

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

public int indexOf(int ch, int fromIndex) {
        final int max = value.length;
        if (fromIndex < 0) {
            fromIndex = 0;
        } else if (fromIndex >= max) {
            // Note: fromIndex might be near -1>>>1.
            return -1;
        }

        if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
            // handle most cases here (ch is a BMP code point or a
            // negative value (invalid code point))
            final char[] value = this.value;
            for (int i = fromIndex; i < max; i++) {
                if (value[i] == ch) {
                    return i;
                }
            }
            return -1;
        } else {
            return indexOfSupplementary(ch, fromIndex);
        }
    }

indexOf(int ch, int fromIndex) method rarely calls private int indexOfSupplementary(int ch, int fromIndex) if unicode greater than 0x010000

private int indexOfSupplementary(int ch, int fromIndex) {
        if (Character.isValidCodePoint(ch)) {
            final char[] value = this.value;
            final char hi = Character.highSurrogate(ch);
            final char lo = Character.lowSurrogate(ch);
            final int max = value.length - 1;
            for (int i = fromIndex; i < max; i++) {
                if (value[i] == hi && value[i + 1] == lo) {
                    return i;
                }
            }
        }
        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(int ch, 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('j'));
		System.out.println("index of java in sub string : " + s.indexOf('j', 25));
		System.out.println("index of ehowtonow : " + s.indexOf('h'));
		System.out.println("index of ehowtonow  in sub string : " + s.indexOf('h', 5));
		System.out.println("index of tutorials : " + s.indexOf('t'));
		System.out.println("index of tutorials  in sub string : " + s.indexOf('t', 35));
	}

}

Output

index of java : 18
index of java in sub string : 56
index of ehowtonow : 1
index of ehowtonow in sub string : 11
index of tutorials : 4
index of tutorials in sub string : 48


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 compareTo() method with example
  2. How to Split the String in Java
  3. How to get Character unicode point in a String
  4. Java String charAt() method with example
  5. Java String Compare
  6. Java String indexOf(String str) method with example
  7. Java String compareToIgnoreCase() method with example
  8. How to format the String by using Locale
  9. Remove the particular character from string
  10. String Concatenation in Java
  11. How to convert a String to Lower case
  12. Java String indexOf(int ch, int fromIndex) method with example
  13. How to convert a String to Upper case
  14. Java String codePointCount() method with example
  15. Java String indexOf(int ch) method with example

Be the first to comment

Leave a Reply

Your email address will not be published.


*