Friday, 5 August 2016

Java String classes


public class HelloWorld {

    public static void main(String args[])
    {

        // Java string class methods        

        /*        String str="Hello World";
        // index finding        System.out.println(str.indexOf("o"));        System.out.println(str.indexOf("o",5));
        // char at index        System.out.println(str.charAt(7));
        //substring        System.out.println(str.substring(3));        System.out.println(str.substring(3,7));        System.out.println(str.contains("S"));
        //is empty        System.out.println(str.isEmpty());        System.out.println("".isEmpty());
        //upper lower        System.out.println(str.toUpperCase());        System.out.println(str.toLowerCase());
        //trim -> removes extra spaces at beg & end
        System.out.println("   hey...!  ".trim());
        // split -> spilts strings into array
        str="apple,nokia,samsung,lenovo";        str.split(",");

        System.out.println(str);
        // join        //str.join(",","apples","oranges,");
     */          String s1="";
        System.out.println(s1);
        System.out.println(s1.length());


        //java.lang.NullPointerException.        // a value was null & you're calling method on that
        /*        s1=null;        System.out.println(s1.length());        */

        s1=null;

        if(s1!=null)
            System.out.println(s1.length());
        else System.out.println("you have null string");



        // concatination of strings
           String s2="";

          s1="hey";
          s2="sree";
          String s3 = s1+" "+s2;
          System.out.println(s3);

        s1="hey";
        s2="hari";
        s3=s1.concat(s2);
        System.out.println(s3);



        // comparison of strings
          s1="Hello";
          s2="Hello";
          s3="hEllo";
          String s4=new String("Hello");

          System.out.println(s1==s2);
          System.out.println(s1==s4);
          System.out.println(s1.equals(s4));
          //use equals method when comparing the strings          //becz it compares the contents of strings

          System.out.println(s1.equalsIgnoreCase(s3));
          //ignores whether capital or small

        // Escape sequences
         s1="\"Hello\"";
         System.out.println(s1);

        s1="\'Hello\'";
        System.out.println(s1);

        s1="\nHello\n";
        System.out.println(s1);

        s1="\tHello\t";
        System.out.println(s1);



        s1="Java Programming";
        System.out.println(s1);
        System.out.println(s1.length());
        char first=s1.charAt(0);

        //  java.lang.StringIndexOutOfBoundsException        /*        char last=s1.charAt(16);        System.out.println(last);         */
        for (int i = 0; i <s1.length() ; i++) {
            System.out.print(s1.charAt(i));
            System.out.print(",");
        }

        System.out.println();

        int index=s1.indexOf('o');
        System.out.println(index);

        //prints -1 if it won't finds        index=s1.indexOf('Z');
        System.out.println(index);

       //index of string as well
        index=s1.indexOf("Pro");
        System.out.println(index);

        // find after skipping some chars
        index=s1.indexOf('a',10);
        System.out.println(index);


        // contains or not (return true or false)
        System.out.println(s1.contains("Pro"));
        System.out.println(s1.contains("pro"));

        // trim -> removes extra spaces        s2="   hey sree!   ";
        System.out.println(s2.trim());
        System.out.println(s2);


        s2="  programming  ";
        s3=s2.trim();
        System.out.println(s2);
        System.out.println(s3);



         s4="sree";
        System.out.println(s4.toUpperCase());
        System.out.println(s4);



        s1="Java Programming";
        s2=s1.substring(3);  //a Programming        System.out.println(s2);

        //  J a v a   P r o g r a m m i n g        //  0 1 2 3 4 5 6 7 8 9 10
        s2=s1.substring(3,6);  // a P        System.out.println(s2);



        // StringIndexOutOfBoundsException        /*        s2=s1.substring(25);  //a Programming        System.out.println(s2);

         */







    }
}

No comments:

Post a Comment