Pages

strings programms

// understanding the strings
class  StringsTest
{
          public static void main(String[] args)
          {
                   // create 3 strings
                   String s1="This is java";
                   String s2=new String(" I like it");
                   char ch[]={'P','o','t','h','u','r','a','i'};
                   String s3= new String(ch);
                   // display the strings
                   System.out.println("S1 = "+s1);
                   System.out.println("S2 = "+s2);
                   System.out.println("S3 = "+s3);
                   // find no . of charecters in s1
                   System.out.println("length of S1 = "+s1.length( ));
                   // join 2 strings
                   System.out.println("S1 joined with s2 = "+s1.concat(s2));
                   // join 3 strings
                   System.out.println(s1+" at "+s3);
                   // check the starting of s1
                   boolean x=s1.startsWith ("This");
                   if(x==true)
                             System.out.println("s1 starts with This ");
                   else
                             System.out.println("S1 does not start with This ");
                   /* extract substring from s1 & s3
                   String p=s2.subString(0,6);
                   String q=s3.subString(0);
                   System.out.println(p+q); */
                   //change the case of s3
      System.out.println("Upper case of s3 = "+s3.toUpperCase( ));
                System.out.println("Lower case of S3 = "+s3.toLowerCase( ));
          }
}
D:\prr\Core java>javac StringsTest.java
D:\prr\Core java>java StringsTest
S1 = This is java
S2 =  I like it
S3 = Pothurai
length of S1 = 12
S1 joined with s2 = This is java I like it
This is java at Pothurai
s1 starts with This
Upper case of s3 = POTHURAI
Lower case of S3 = pothurai
// equality of strings
class StringEqual
{
public static void main(String args[])
{
String a="Hello";
String b=new String("Hello");
if(a==b)
System.out.println("same");
else
System.out.println("Not same");
}
}
D:\prr\Core java>javac StringEqual.java
D:\prr\Core java>java StringEqual

Not same