Java String indexOf(int ch) method with example

Java String indexOf(int ch) method returns the index within this string of the first occurrence of the specified character. If a character with value ch occurs in the character sequence represented by this String object, then the index (in Unicode code units) 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
is true. For other values of ch, it is the smallest value k such that: this.codePointAt(k) == ch
is true. In either case, if no such character occurs in this string, then -1 is returned.

Method Signature

public int indexOf(int ch)

Parameters:

ch – a character (Unicode code point).

Returns:

the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.

String indexOf(int ch) method internal implementation

 public int indexOf(int ch) {
        return indexOf(ch, 0);
    }

indexOf(int ch) method internally calls public int indexOf( int ch , int fromIndex) method. 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) method example

package com.ehowtonow.java.lang.string;

public class IndexOfExample {

	public static void main(String[] args) {

		String s = "This is ehowtonow java tutorials";
		System.out.println("index of j : " + s.indexOf('j'));
		System.out.println("index of e : "+ s.indexOf('e'));
		System.out.println("index of e : "+ s.indexOf('t'));
		System.out.println("index of a : "+ s.indexOf('a'));
	}

}

Output

index of j : 18
index of e : 8
index of e : 12
index of a : 19


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 indexOf(String str, int fromIndex) method with example
  2. Java String codePointAt() method with example
  3. Java String copyValueOf(char data[]) method with example
  4. Java String concat() method with example
  5. Java String contentEquals(StringBuffer sb) method with example
  6. Java String Compare
  7. How to convert a String to Lower case
  8. Java String indexOf(int ch, int fromIndex) method with example
  9. Java String contains() method with example
  10. How to get Character unicode point in a String
  11. Java String compareToIgnoreCase() method with example
  12. Java String contentEquals(CharSequence cs) method with example
  13. Java String compareTo() method with example
  14. Java String charAt() method with example
  15. How to find last occurrence of substring inside string

Be the first to comment

Leave a Reply