Page 311 - ComputerScience_Class_11
P. 311

3      public static void main(String[] args)
                   4      {

                   5      StringBuffer s1= new StringBuffer("Computer");
                   6      s1.insert(2, "MAN");
                   7      System.out.println(s1);

                   8      }
                   9      }

                 The output of the preceding program is as follows:

                       BlueJ: Terminal Window - Java

                   Options
                  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:

                                 Write a program to delete a part of a string using the delete() method in a StringBuffer and
                   Program 26
                                 print the result.
                   1      class stringDelete

                   2      {
                   3      public static void main(String[] args)

                   4      {
                   5      StringBuffer s1= new StringBuffer("I am not going to play");

                   6      s1.delete(5,8);
                   7      System.out.println(s1);

                   8      }
                   9      }

                 The output of the preceding program is as follows:

                       BlueJ: Terminal Window - Java
                   Options

                  I am  going to play




                        Note:  There are two spaces between “am going” as only the word “not” is removed.





                                                                                                           Strings  309
   306   307   308   309   310   311   312   313   314   315   316