Custom Search

Saturday, 25 August 2012

Java Program that illustrate String class using different methods like equal, compareTo, replace, lowercase, uppercase, concat, valueof, indedof etc.


Java Program that illustrate String class using different methods like equal, compareTo, replace, lowercase, uppercase, concat, valueof, indedof etc.


Program
class StCls
{
public static void main(String args[])
{
String str1="I Love My India";
String str2=new String("I Love My India");
String str3="I Love My India";
String str4="Teach";
String str5="YourSelf";
StringBuffer sb=new StringBuffer("I Love My India");
System.out.println("str1==str2:"+ (str1==str2));
System.out.println("str1==str3:"+ (str1==str3));
        int result = str1.compareTo(str2);
      if (result < 0)
           System.out.println(str1 + " is less than "  + str2);
        else if (result == 0)
System.out.println(str1 +" is equal to " + str2);
            else 
        System.out.println(str1 +" is greater than " + str2);
        //Concat
String str6=str4.concat(str5);
System.out.println("Concated String is: "+ str6);
    //Replace
System.out.println("String After Replace:"+ sb.replace(10,16,"Country"));
//Lowercase
String lower=str4.toLowerCase();
System.out.println("String After Conversion:"+ lower);
//Uppercase
String upper=str4.toUpperCase();
System.out.println("String After Conversion:"+ upper);
int i=str4.indexOf("c");
System.out.println("The Index of c is :"+i);
}
}

No comments:

Post a Comment

Laptops