Source of StringStartEnd.java


  1: // Fig. 29.4: StringStartEnd.java
  2: // String methods startsWith and endsWith.
  3: 
  4: public class StringStartEnd 
  5: {
  6:    public static void main( String args[] )
  7:    {
  8:       String strings[] = { "started", "starting", "ended", "ending" };
  9: 
 10:       // test method startsWith
 11:       for ( String string : strings )
 12:       {
 13:          if ( string.startsWith( "st" ) )
 14:             System.out.printf( "\"%s\" starts with \"st\"\n", string );
 15:       } // end for
 16: 
 17:       System.out.println();
 18: 
 19:       // test method startsWith starting from position 2 of string
 20:       for ( String string : strings )
 21:       {
 22:          if ( string.startsWith( "art", 2 ) ) 
 23:             System.out.printf( 
 24:                "\"%s\" starts with \"art\" at position 2\n", string );
 25:       } // end for
 26: 
 27:       System.out.println();
 28: 
 29:       // test method endsWith
 30:       for ( String string : strings )
 31:       {
 32:          if ( string.endsWith( "ed" ) )
 33:             System.out.printf( "\"%s\" ends with \"ed\"\n", string );
 34:       } // end for
 35:    } // end main
 36: } // end class StringStartEnd
 37: 
 38: /**************************************************************************
 39:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 40:  * Pearson Education, Inc. All Rights Reserved.                           *
 41:  *                                                                        *
 42:  * DISCLAIMER: The authors and publisher of this book have used their     *
 43:  * best efforts in preparing the book. These efforts include the          *
 44:  * development, research, and testing of the theories and programs        *
 45:  * to determine their effectiveness. The authors and publisher make       *
 46:  * no warranty of any kind, expressed or implied, with regard to these    *
 47:  * programs or to the documentation contained in these books. The authors *
 48:  * and publisher shall not be liable in any event for incidental or       *
 49:  * consequential damages in connection with, or arising out of, the       *
 50:  * furnishing, performance, or use of these programs.                     *
 51:  *************************************************************************/