Showing posts with label String functions. Show all posts
Showing posts with label String functions. Show all posts

Thursday, 13 June 2013

Searching strings- indexof() method

The string class provide two methods that allow to search a string for a specific character or substring...

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

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:



Wednesday, 12 June 2013

String Compare function

String compare function is used to sort a string by name. The string entered by user is scanned word by word. This function arrange the string in alphabetic order.
If the second word appear earlier to first word, then this function generates a negative value i.e. less than Zero.
If the second word appear after the first word, then this function generates a positive value i.e. greater than Zero.
If the second word is same to the first word, then this function generates Zero.

Program for string compare function:


class Strcomp
{
public static void main(String arg[])
{
String arr[]={"this","is","the","duty","of","every","citizen","to","serve","his","country"};
String t;
int i,j;
for(i=0;i<arr.length;i++)
{
for(j=i+1;j<arr.length;j++)
{
if(ar[j].compareTo(arr[i])<0)
{
t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
}
}
for(i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
}

Following is the output of the above code:



full details in json
Proudly Powered by Blogger.
back to top