Source of StringMiscellaneous2.java


  1: // Fig. 29.8: StringMiscellaneous2.java
  2: // String methods replace, toLowerCase, toUpperCase, trim and toCharArray.
  3: 
  4: public class StringMiscellaneous2 
  5: {
  6:    public static void main( String args[] )
  7:    {
  8:       String s1 = new String( "hello" );
  9:       String s2 = new String( "GOODBYE" );
 10:       String s3 = new String( "   spaces   " );
 11: 
 12:       System.out.printf( "s1 = %s\ns2 = %s\ns3 = %s\n\n", s1, s2, s3 );
 13: 
 14:       // test method replace      
 15:       System.out.printf( 
 16:          "Replace 'l' with 'L' in s1: %s\n\n", s1.replace( 'l', 'L' ) );
 17: 
 18:       // test toLowerCase and toUpperCase
 19:       System.out.printf( "s1.toUpperCase() = %s\n", s1.toUpperCase() );
 20:       System.out.printf( "s2.toLowerCase() = %s\n\n", s2.toLowerCase() );
 21: 
 22:       // test trim method
 23:       System.out.printf( "s3 after trim = \"%s\"\n\n", s3.trim() );
 24: 
 25:       // test toCharArray method
 26:       char charArray[] = s1.toCharArray();
 27:       System.out.print( "s1 as a character array = " );
 28: 
 29:       for ( char character : charArray )
 30:          System.out.print( character );
 31: 
 32:       System.out.println();
 33:    } // end main
 34: } // end class StringMiscellaneous2
 35: 
 36: /**************************************************************************
 37:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 38:  * Pearson Education, Inc. All Rights Reserved.                           *
 39:  *                                                                        *
 40:  * DISCLAIMER: The authors and publisher of this book have used their     *
 41:  * best efforts in preparing the book. These efforts include the          *
 42:  * development, research, and testing of the theories and programs        *
 43:  * to determine their effectiveness. The authors and publisher make       *
 44:  * no warranty of any kind, expressed or implied, with regard to these    *
 45:  * programs or to the documentation contained in these books. The authors *
 46:  * and publisher shall not be liable in any event for incidental or       *
 47:  * consequential damages in connection with, or arising out of, the       *
 48:  * furnishing, performance, or use of these programs.                     *
 49:  *************************************************************************/