Page 244 - computer science (868) class 11
P. 244

Output:
              Computer Science


                   9.3 STRINGBUFFER CLASS
              The StringBuffer class is used to create sufficient space in the memory to store a String. This space in memory can be
              changed as required. If the string is less than the size defined, the extra space will contain an extra value. Let us learn
              about some of the commonly used methods of the StringBuffer class.


              9.3.1 The append() Method
              We use the append() method to concatenate a string at the end of another string. The syntax of the append() method is:

                  StringBuffer string_object.append(StringBuffer variable);
              Let us see the below example:

                  StringBuffer s1= new StringBuffer("Computer");
                  StringBuffer s2= new StringBuffer("Science") ;
                  s1.append(s2);
                  System.out.println(s1);
              Output:

              ComputerScience

              The setCharAt() Method
              We use the setCharAt() method to replace one character with another character. The syntax of the setCharAt() method is:
                  StringBuffer string_object.setCharAt(index value,Character);
              Let us see the below example:

                  StringBuffer s1= new StringBuffer("Computer");
                  s1.setCharAt(2,'M');
                  System.out.println(s1);
              Output:
              CoMputer

              The insert() Method
              We use the insert() method to insert a string at the specified index into another string. The syntax of the insert() method is:

                  StringBuffer string_object.index(index value,StringBuffer Variable);
              Let us see the below example:

                  StringBuffer s1= new StringBuffer("Computer");
                  s1.insert(2, "MAN");
                  System.out.println(s1);
              Output:

              CoMANmputer

              The delete() Method
              We use the delete() method to delete a range of characters starting from one index to another in a string. The syntax
              of the delete() method is:
                  StringBuffer string_object.index(startindex value,endindex_value);
              Let us see the below example:
                  StringBuffer s1= new StringBuffer("I am not going to play");
                  s1.delete(5,8);
                  System.out.println(s1);


                242242  Touchpad Computer Science-XI
   239   240   241   242   243   244   245   246   247   248   249