Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - String indexOf() method



indexOf() method returns the index within this String of the first occurrence of the specified substring. This method has 4 different variants.

  • public int indexOf(char ch) − Returns the index within this string of the first occurrence of the specified character or -1 if the character does not occur.

Syntax

public int indexOf(char ch)

Parameters

ch − The character to search for in the string.

Return Value

Returns the index within this string of the first occurrence of the specified character or -1 if the character does not occur.

  • public int indexOf(char ch, int fromIndex) − Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index or 1 if the character does not occur.

Syntax

public int indexOf(char ch, int fromIndex)

Parameters

  • ch − The character to search for in the string

  • fromIndex − where to start the search from

Return Value

Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index or -1 if the character does not occur.

  • int indexOf(String str) − Returns the index within this string of the first occurrence of the specified substring. If it does not occur as a substring, -1 is returned.

Syntax

int indexOf(String str)

Parameters

str − The string to search for

Return Value

Returns the index within this string of the first occurrence of the specified substring. If it does not occur as a substring, -1 is returned.

  • 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. If it does not occur, -1 is returned.

Syntax

int indexOf(String str, int fromIndex)

Parameters

  • str − The string to search for

  • fromIndex − where to start the search from

Return Value

Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. If it does not occur, -1 is returned.

Example - Usage of indexOf(char) method

Following is an example of the usage of indexOf(char) method.

Example.groovy

class Example { 
   static void main(String[] args) { 
      String a = "Hello World"; 
		
      // Using public int indexOf(int ch) 
      println(a.indexOf('e')); 
      println(a.indexOf('o')); 
   } 
}

Output

When we run the above program, we will get the following result −

1 
4 

Example - Usage of indexOf(char, int) method

Following is an example of the usage of indexOf(char, int) method.

Example.groovy

class Example { 
   static void main(String[] args) { 
      String a = "Hello World"; 
		
      // Using public int indexOf(char ch, int fromIndex) 
      println(a.indexOf('l',1)); 
      println(a.indexOf('e',4));
   } 
}

Output

When we run the above program, we will get the following result −

 
2 
-1 

Example - Usage of indexOf(String) method

Following is an example of the usage of indexOf(String) method.

Example.groovy

class Example { 
   static void main(String[] args) { 
      String a = "Hello World"; 
		
      // Using public int indexOf(string str) 
      println(a.indexOf("el")); 
      println(a.indexOf("or")); 
   } 
}

Output

When we run the above program, we will get the following result −

1 
7 

Example - Usage of indexOf(String, int) method

Following is an example of the usage of indexOf(String,int) method.

Example.groovy

class Example { 
   static void main(String[] args) { 
      String a = "Hello World"; 
		
      // Using public int indexOf(string str,int fromIndex) 
      println(a.indexOf("el",1)); 
      println(a.indexOf("or",8)); 
   } 
}

Output

When we run the above program, we will get the following result −

 
1 
-1
groovy_strings.htm
Advertisements