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

Be the first to comment

Leave a Reply

Your email address will not be published.


*