Java String indexOf()

Java String indexOf() Method

In this tutorial, we will learn about string java indexof method and its Syntax and Programming Examples to find the Java of Characters or Strings. String indexOf() Java Method is used to get the index of the first occurrence of criteria specified in the parameters of the IndexOf method.

A common scenario can be when a system admin wants to find the index of the β€˜@’ character of the email Id of a client and then wants to get the remaining substring. In that situation, the IndexOf method can be used.

The Java String indexOf method returns the index of the first occurrence of a specified substring within a string.

Syntax :

There are some following Syntaxes available in Java String indexof-

  •   public int indexOf(String str)
  •   public int indexOf(String str, int fromIndex)
  •   public int indexOf(int char)
  •   public int indexOf(int char, int fromIndex

Parameters :

Below are the indexOf string java parameters:

  • char: Used to represent a single character value
  • str: Used to represent the string to search for
  • fromIndex: Used to represent the index position to start the search from

IndexOf(char b)

This method returns the index of the character β€˜b’ passed as a parameter. If that character is not available in the string, the returned index would be -1.

IndexOf(char c, int startindex)

The given method would return the index of the first occurrence of character β€˜c’ after the integer index passed as the second parameter β€œstartindex.” All the occurrences of character β€˜c’ before the β€œstartindex” integer index would be ignored.

IndexOf(String substring)

The above Java substring indexOf() method returns the index of the first character of the substring passed as a parameter to it. If that substring is not available in the string, the returned index would be -1.

IndexOf(String substring, int startindex)

This Java substring indexOf() method returns the index of the first character in the substring passed as the first parameter, after the β€œstartindex” index value. If substring starts from the passed integer value of β€œstartindex”, that substring would be ignored.

Java String IndexOf() method returns the position or index of the given character or substring. This method returns positive integer value when given character or substring has found. In the case of not found given character or substring, indexOf() returns a negative number.  

Note: index starts from 0 (Zero).

There are four types of indexOf() method in Java The Syntax of indexOf() methods are as follows :

int indexOf(int ch) : ch indicatess any character or unicode of particular character.

int indexOf(int ch, int startFrom) : ch indicatess any character or unicode of particular character. startFrom indicates the start finding from given integer value.

int indexOf(String str) : str indicates any string.

int indexOf(String str,int startFrom) : str indicates any string.startFrom indicates the start finding from given integer value.

The method searches the java string index from the beginning and returns the index of the first match.

Java indexOf() Examples

1. In this example, we will give a character. This method will return the position of that character in the given string.

class IndexOfExample{
  public static void main(String []args){
    String fullStr = "DEVELOPER HELPS IS A PRORAMMING WEBSITE";
    System.out.println("Index of given character is:"+fullStr.indexOf('L'));
  }
}

Output :

Index of given character is:4

2. In Java String indexOf() example, we will give a character and start from the value. This method will return the position of that character in the given string.

class IndexOfExample{
  public static void main(String []args){
  String fullStr = "DEVELOPER HELPS IS A PRORAMMING WEBSITE";
  System.out.println("Index of given character is:"+fullStr.indexOf('L',6));
  }
}

Output :

Index of given character is: 12

3. In this example, we will give a String. This method will return the position of that String in the given String.

class IndexOfExample{
  public static void main(String []args){
   String fullStr = "DEVELOPER HELPS IS A PRORAMMING WEBSITE";
   System.out.println("Index of given character is:"+fullStr.indexOf("LO"));
  }
}

Output :

Index of given character is:4

4. In this example, we will give a String and start from a value. This method will return the position of that String in the given String.

class IndexOfExample{
 public static void main(String []args){
 String fullStr = "DEVELOPER HELPS IS A PRORAMMING WEBSITE";
 System.out.println("Index of given character is:"+fullStr.indexOf("EL",5));
 }
}

Output :

Index of given character is:11

5. Let us consider a programming code of string.indexof java

public class Sample_String {
public static void main(String args[]) {
        String str_Sample = "This is Index of Example";
        //Character at position
        System.out.println("Index of character 'x': " + str_Sample.indexOf('x'));
        //Character at position after given index value
        System.out.println("Index of character 's' after 3 index: " + str_Sample.indexOf('s', 3));
        //Give index position for the given substring
        System.out.println("Index of substring 'is': " + str_Sample.indexOf("is"));
        //Give index position for the given substring and start index
        System.out.println("Index of substring 'is' form index:" + str_Sample.indexOf("is", 5));
    }
}

Output :

Index of character 'x': 12
Index of character 's' after 3 index: 3
Index of substring 'is': 2
Index of substring 'is' form index:5

Java indexOf() return value

returns the index of the first occurrence of the specified character/string

returns -1 if the specified character/string is not found.

More Related Post

Thanks for the reading post. I hope you like and understand the post. If you have any doubt regarding this post please comment below.

https://www.facebook.com/developerhelps/

Leave a comment

Your email address will not be published. Required fields are marked *