Tuesday, 9 August 2016

Stirng builder


public class HelloWorld {

    public static void main(String args[])
    {

        // String Builders

        StringBuilder sb= new StringBuilder();
        sb.append("Hello");
        System.out.println(sb);

        //string object can't modified        //string builder modifies original string
          StringBuilder s1=new StringBuilder("hello again!");
         System.out.println(s1);


        // specify intial capacity 5 chars in string build
         StringBuilder s2=new StringBuilder(5);
        System.out.println(s2);
        s2.append("hey back to java programming");
        System.out.println(s2);

        //capacity is flexible
        //find length        System.out.println(s2.length());


        //concatination          StringBuilder s3=new StringBuilder("Hello");
          s3.append("world");
        System.out.println(s3);

        s3.append("!");
        System.out.println(s3);



        // methods
        StringBuilder s4=new StringBuilder("Hello again");
        System.out.println(s4);


       //insert
        s4.insert(5,"2");
        System.out.println(s4);


        //replace
        s4.replace(5,s4.length()," world");
        System.out.println(s4);

        //delete        s4.delete(5,s4.length());
        System.out.println(s4);


        s4.append("abcdef ghijkl");
        System.out.println(s4);

        s4.delete(8,14);        /// Helloabcdef ghijkl        System.out.println(s4); // Helloabcijkl
        // delete char at
        System.out.println(s4.length());
          //s4.deleteCharAt(s4.lenght());

        //java.lang.StringBuilder.deleteCharAt         // here length is 12: indexes(0->11)        /*        s4.deleteCharAt(12);        System.out.println(s4);          */
        s4.deleteCharAt(11);
        System.out.println(s4);


           String s5="Hello world";
          for(int i=0;i<s5.length();i++)
          {
              System.out.println(s5.charAt(i));

          }




        String s6= "Hello from ";

        /// it won't modify the existing string by concatination        //we need to store the info in other string.
          String s8=s6.concat("sree");
        StringBuilder s7 = new StringBuilder("hey sree ");
        s7.append("wzzup");

        System.out.println(s6);
        System.out.println(s7);
        System.out.println(s8);


        s7.deleteCharAt(0);
        System.out.println(s7);

        //insert        s7.insert(0,"hie h");
        System.out.println(s7);

        // delete
         s7.delete(0,3);
        System.out.println(s7);


        /// repalce
          s7.replace(0,3,"hieee");
        System.out.println(s7);




        //converting to string
        int no=1024;
        String text=String.valueOf(no);
        System.out.println(text);

         System.out.println(text.substring(0,1));


          String flt=String.valueOf(24.56f);
          String dbl=String.valueOf(24.56);
          String bool=String.valueOf(true);

        System.out.println(flt);
        System.out.println(dbl);
        System.out.println(bool);


         String num="12345";
        System.out.println(num);
         int nob=Integer.parseInt(num);
        System.out.println(nob);

         //doble.parsedouble        //float.parse        //Boolean.parse

    }
}

No comments:

Post a Comment