Java - String indexOf() Method



Description

The 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(Unicode code units) of the first such occurrence is returned.

Declaration

Following is the declaration for Java String indexOf() method

public int indexOf(int ch)

Parameters

ch − This is a character (Unicode code point).

Return Value

This method 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.

Exception

NA

String indexOf(int ch, int fromIndex) Method

Description

The 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.

Declaration

Following is the declaration for Java String indexOf() method

public int indexOf(int ch, int fromIndex)

Parameters

  • ch − This is a character (Unicode code point).

  • fromIndex − This is the index to start the search from.

Return Value

This method 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.

Exception

NA

String indexOf(String str) Method

Description

The Java String indexOf(String str) method returns the index within this string of the first occurrence of the specified substring. The integer returned is the smallest value k such that: this.startsWith(str, k) is true.

Declaration

Following is the declaration for Java String indexOf() method

public int indexOf(String str)

Parameters

str − This is value of a string.

Return Value

This method returns if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.

Exception

NA

String indexOf(String str, int fromIndex) Method

Description

The 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 integer returned is the smallest value k for which −

k > = Math.min(fromIndex, this.length()) && this.startsWith(str, k)

If no such value of k exists, then -1 is returned.

Declaration

Following is the declaration for Java String indexOf() method

public int indexOf(String str, int fromIndex)

Parameters

  • str − This is the substring for which to search.

  • fromIndex − This is the index from which to start the search.

Return Value

This method returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

Exception

NA

Getting Index of a Char from a String Example

The following example shows the usage of Java String indexOf() method. We created a string literal with the value "This is tutorialspoint". Then using the indexOf() method, we are retrieving the index Of e.

package com.tutorialspoint;

public class StringDemo {

   public static void main(String[] args) {

      String str = "This is tutorialspoint";
       
      // returns the index of occurrence of character s
      System.out.println("index of letter 's' = " 
         + str.indexOf('s')); 
      
      // returns -1 as character e is not in the string
      System.out.println("index of letter 'e' = " 
         + str.indexOf('e'));
   }
}

Let us compile and run the above program, this will produce the following result −

index of letter 's' = 3
index of letter 'e' = -1

Getting Index of a String from a String Example

The following example shows the usage of Java String indexOf() method. We created a string literal with the value "This is tutorialspoint". Then using the indexOf() method, we are retrieving the index Of e.

package com.tutorialspoint;

public class StringDemo {

   public static void main(String[] args) {
 
      String str = "This is tutorialspoint";

      // returns positive value as character is located
      System.out.println("index of letter 't' =  "
         + str.indexOf('t', 14)); 
      
      // returns positive value as character is located
      System.out.println("index of letter 's' =  "
         + str.indexOf('s', 10)); 
      
      // returns -1 as character is not in the string
      System.out.println("index of letter 'e' =  "
         + str.indexOf('e', 5));
   }
}

Let us compile and run the above program, this will produce the following result −

index of letter 't' = 21
index of letter 's' = 16
index of letter 'e' = -1

Getting Index of a Char from a String Example

The following example shows the usage of Java String indexOf() method. We created a string literal with the value "Collections of tutorials at tutorials point". Then using the indexOf() method, we are retrieving the index Of tutorials and admin.


package com.tutorialspoint;

public class StringDemo {

   public static void main(String[] args) {

      String str1 = "Collections of tutorials at tutorials point";
     
      // returns index of first character of the substring "tutorials" 
      System.out.println("index =  " + str1.indexOf("tutorials")); 
      
      // returns -1 as substring "admin" is not located
      System.out.println("index =  " + str1.indexOf("admin"));    
   }
}

Let us compile and run the above program, this will produce the following result −

index = 15
index = -1

Getting Index of a String from a String with FromIndex Example

The following example shows the usage of Java String indexOf() method. We created a string literal with the value "Collections of tutorials at tutorials point". Then using the indexOf() method, we are retrieving the index Of tutorials and admin.

package com.tutorialspoint;

public class StringDemo {

   public static void main(String[] args) {

      String str1 = "Collections of tutorials at tutorials point";
     
      /*  search starts from index 10 and if located it returns 
         the index of the first character of the substring "tutorials" */
      System.out.println("index = " + str1.indexOf("tutorials", 10)); 
      
      /* search starts from index 9 and returns -1 as substring "admin"
         is not located */
      System.out.println("index = " + str1.indexOf("admin", 9));     
   }
}

Let us compile and run the above program, this will produce the following result −

index = 15
index = -1
java_lang_string.htm
Advertisements