Source of StringMiscellaneous.java


  1: // Fig. 29.2: StringMiscellaneous.java
  2: // This application demonstrates the length, charAt and getChars
  3: // methods of the String class.
  4: 
  5: public class StringMiscellaneous 
  6: {
  7:    public static void main( String args[] )
  8:    {
  9:       String s1 = "hello there";
 10:       char charArray[] = new char[ 5 ];
 11: 
 12:       System.out.printf( "s1: %s", s1 );
 13: 
 14:       // test length method
 15:       System.out.printf( "\nLength of s1: %d", s1.length() );
 16: 
 17:       // loop through characters in s1 with charAt and display reversed
 18:       System.out.print( "\nThe string reversed is: " );
 19: 
 20:       for ( int count = s1.length() - 1; count >= 0; count-- )
 21:          System.out.printf( "%s ", s1.charAt( count ) );
 22: 
 23:       // copy characters from string into charArray
 24:       s1.getChars( 0, 5, charArray, 0 );
 25:       System.out.print( "\nThe character array is: " );
 26: 
 27:       for ( char character : charArray )
 28:          System.out.print( character );
 29: 
 30:       System.out.println();
 31:    } // end main
 32: } // end class StringMiscellaneous
 33: 
 34: /**************************************************************************
 35:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 36:  * Pearson Education, Inc. All Rights Reserved.                           *
 37:  *                                                                        *
 38:  * DISCLAIMER: The authors and publisher of this book have used their     *
 39:  * best efforts in preparing the book. These efforts include the          *
 40:  * development, research, and testing of the theories and programs        *
 41:  * to determine their effectiveness. The authors and publisher make       *
 42:  * no warranty of any kind, expressed or implied, with regard to these    *
 43:  * programs or to the documentation contained in these books. The authors *
 44:  * and publisher shall not be liable in any event for incidental or       *
 45:  * consequential damages in connection with, or arising out of, the       *
 46:  * furnishing, performance, or use of these programs.                     *
 47:  *************************************************************************/