Sunday, 7 December 2014

WAP which will help us to understand how to create strings and how to use some important methods of String class.

//StrDemo.java
class StrDemo
{

public static void main(String[] args)
{
// creating string in 3 ways
String s1="The book on java";
String s2=new String(" i like it");
char arr[]={ 'D','r','e','a','m','t','e','c','h'};
String s3=new String(arr);
///Display all the three string
 
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);

       // find length of first string
   
System.out.println("Length of S1 ="+s1.length());

       //concatinate two strings

System.out.println("s1 and s2 joined ="+s1.concat(s2));
   
       //concatinate three strings with +
    System.out.println(s1+ " from "+s3);

// test if string s1 starts with A
             boolean x=s1.startsWith("A");

if(x)
        System.out.println("s1 starts with \'A\' ");
               else
         System.out.println("s1 does not starts with \'A\' ");
               
//extract substring from s2, starting from 0th chat to 6th char

String p =s2.substring(0,7);
                 System.out.println(p);

//extract substring from s3, starting from 0th char to 8th char
 
   String q=s3.substring(0,9);
                System.out.println(q);

//concatenate the strings p and q
System.out.println(p +" "+q);

//convert s1 in to upper cast and lower cast

                System.out.println("Upper s1= "+s1.toUpperCase());
                System.out.println("Lower s1= "+s1.toLowerCase());


   
}
}

No comments:

Post a Comment