java String

Java String indexof() Code With Examples

Table of Contents

Program

public class  StringIndex
{
         	     public static void main(String args[])
		     {	
                            String s1 = new String("hello world");
                            int value1=s1.indexOf('h');
			    System.out.println(value1);
			    int value2=s1.indexOf('r',4);
			    System.out.println(value2);
			    int value3=s1.indexOf("world");
                            System.out.println(value3);
		            int value4=s1.indexOf("world",3);
			    System.out.println(value4);
			    int value5 = s1.lastIndexOf('d');  
                            System.out.println(value5);
			    int value6 = s1.lastIndexOf('w',7);
			    System.out.println(value6);
			    int value7 = s1.lastIndexOf("world");
		            System.out.println(value7);
		     }
				

}

Output

		   0
		   8
		   6
		   6
		   10
		   6
		   6

Description

public int indexOf(int ch)

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.

Parameters:

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.

public int indexOf(int ch, int fromIndex)

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

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.

public int indexOf?(String str)

Returns the index within this string of the first occurrence of the specified substring.
The returned index is the smallest value k for which:this.startsWith(str, k) If no such value of k exists, then -1 is returned.

Parameters:

the substring to search for.

Returns:

the index of the first occurrence of the specified substring, or -1 if there is no such occurrence.

public int indexOf(String str, int fromIndex)

Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
The returned index 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.

Parameters:

str – the substring to search for.
fromIndex – the index from which to start the search.

Returns:

the index of the first occurrence of the specified substring, starting at the specified index, or -1 if there is no such occurrence.

public int lastIndexOf(int ch)

Returns the index within this string of the last occurrence of the specified character. For values of ch in the range from 0 to 0xFFFF (inclusive), the index (in Unicode code units) returned is the largest value k such that:
this.charAt(k) == ch is true. For other values of ch, it is the largest 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. The String is searched backwards starting at the last character.

Parameters:

a character (Unicode code point).

Returns:

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

public int lastIndexOf(int ch, int fromIndex)

Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index. For values of ch in the range from 0 to 0xFFFF (inclusive), the index returned is the largest value k such that:
(this.charAt(k) == ch) && (k <= fromIndex) is true. For other values of ch, it is the largest 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 before position fromIndex, then -1 is returned.
All indices are specified in char values (Unicode code units).

Parameters:

ch – a character (Unicode code point).fromIndex – the index to start the search from. There is no restriction on the value of fromIndex. If it is greater than or equal to the length of this string, it has the same effect as if it were equal to one less than the length of this string: this entire string may be searched. If it is negative, it has the same effect as if it were -1: -1 is returned.

Returns:

the index of the last occurrence of the character in the character sequence represented by this object that is less than or equal to fromIndex, or -1 if the character does not occur before that point.

public int lastIndexOf(String str, int fromIndex)

Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.
The returned index is the largest 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.

Parameters:

str – the substring to search for.
fromIndex – the index to start the search from.

Returns:

the index of the last occurrence of the specified substring, searching backward from the specified index, or -1 if there is no such occurrence.