Friday, 17 July 2015

STRING CLASS FUNCTIONS IN JAVA WITH EXAMPLES


String class: It is a predefined class in java which contains some predefined functions to perform different operations on string. We can call all those functions in our programs by creating a reference to the String class.
Predefined string functions in java useful for testing are as follows
Length (): finds the length of the string
Equals (): compares two strings, returns
 True if both are matching else returns
 False
subString(): selects a part of a string
indexOf():finds a word or a char in the
 string then returns index of the string ,
 if not find then returns -1
split(): splitting a string


public class strng {

         
          public static void main(String[] args) {
                   String s="a b c d";
                   String s1= "india is a beautiful   country";
                   String s2="INDIA IS A BEAUTIFUL    COUNTRY";
                   System.out.println(s1.equals(s2));
                  
                   System.out.println(s1.length());
      System.out.println(s2.length());
     
      System.out.println(s1.substring(0,15));
      System.out.println(s2.substring(0,31));
     
     
System.out.println(s1.toUpperCase());
System.out.println(s2.toLowerCase());

System.out.println(s1.replace( 'i', 'j'));
System.out.println(s2.replace('I','J'));
int i=s1.indexOf("india");
//System.out.println(i);
if (i!=-1)
{
          System.out.println("passed");
}
else
{
          System.out.println("failed");
}
String data []= s1.split("()");
for (int j=0;j<data.length;j++)
{
          System.out.println(data[j]);
}
String data1 []= s2.split("()");
for (int k=0;k<data1.length;k++)
{
          System.out.println(data1[k]);
}



}
}


No comments:

Post a Comment