indexof();
lastIndexOf();
To search for a specific character in the string , indexOf() function is used.This function returns an integer type value ,it returns the first occurrence of the specified character in the string.The charcter should be written in single quote
Syntax: Stringname.indexOf('ChararacterToBeSearched');
The same function can be used to search for a particular string in a complete string.The string is always written in double quotes.
Syntax : Stringname.indexOf("StringToBeSearched");
If you want to search for a character after a particular number of characters, then the start index is also given along with the character, when the program is compiled by Java complier , compiler donot search for the specified character upto that index number specified by you.
Syntax: Stringname.indexOf('ChararacterToBeSearched',StartIndex);
In the same way if you want to search a string or a word in the string to be searched then the start index is given along with the string to be searched. Compler during compiling donot search forthe string upto that index numbered entered.
Syntax:: Stringname.indexOf("StringToBeSearched",StartIndex);
import java.util.*;
class Searchstr
{
public static void main(String arg[])
{
String s= "this is the duty of every citizen of india to serve his country at any cost";
System.out.println(s);
System.out.print("\n\nIndex of(o) = ");
System.out.println(s.indexOf('o'));
System.out.print("Index of(the) = ");
System.out.println(s.indexOf("the"));
System.out.print("Index of('o',10) = ");
System.out.println( s.indexOf('o',10));
System.out.print("Index of(the,10) = ");
System.out.println( s.indexOf("the",10));
}
}
Below is the output of above code:
Syntax: Stringname.indexOf('ChararacterToBeSearched');
The same function can be used to search for a particular string in a complete string.The string is always written in double quotes.
Syntax : Stringname.indexOf("StringToBeSearched");
If you want to search for a character after a particular number of characters, then the start index is also given along with the character, when the program is compiled by Java complier , compiler donot search for the specified character upto that index number specified by you.
Syntax: Stringname.indexOf('ChararacterToBeSearched',StartIndex);
In the same way if you want to search a string or a word in the string to be searched then the start index is given along with the string to be searched. Compler during compiling donot search forthe string upto that index numbered entered.
Syntax:: Stringname.indexOf("StringToBeSearched",StartIndex);
Program to demonstrate the use of indexOf() unction
import java.io.*;import java.util.*;
class Searchstr
{
public static void main(String arg[])
{
String s= "this is the duty of every citizen of india to serve his country at any cost";
System.out.println(s);
System.out.print("\n\nIndex of(o) = ");
System.out.println(s.indexOf('o'));
System.out.print("Index of(the) = ");
System.out.println(s.indexOf("the"));
System.out.print("Index of('o',10) = ");
System.out.println( s.indexOf('o',10));
System.out.print("Index of(the,10) = ");
System.out.println( s.indexOf("the",10));
}
}
Below is the output of above code: