Page 309 - ComputerScience_Class_11
P. 309

Let us see the below example:
                                 Write a program to remove the leading and trailing spaces from a string using the trim()
                   Program 22
                                 method.
                   1      class stringTrim
                   2      {

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

                   5      String sen1= " Computer Science ";
                   6      System.out.println(sen1.trim());

                   7      }
                   8      }


                 The output of the preceding program is as follows:
                       BlueJ: Terminal Window - Java

                   Options

                  Computer Science



                     10.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.


                 10.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:

                   Program 23    Write a program to append one StringBuffer to another and print the result.

                   1      class stringAppend

                   2      {

                   3      public static void main(String[] args)
                   4      {
                   5      StringBuffer s1= new StringBuffer("Computer");

                   6      StringBuffer s2= new StringBuffer("Science");
                   7      s1.append(s2);

                   8      System.out.println(s1);
                   9      }
                  10      }




                                                                                                           Strings  307
   304   305   306   307   308   309   310   311   312   313   314