stringstartend.java

来自「金旭亮的java教案」· Java 代码 · 共 45 行

JAVA
45
字号
// StringStartEnd.java
// This program demonstrates the methods startsWith and
// endsWith of the String class.
import javax.swing.*;

public class StringStartEnd {
   public static void main( String args[] )
   {
      String strings[] = { "started", "starting", 
                           "ended", "ending" };
      String output = "";

      // Test method startsWith
      for ( int i = 0; i < strings.length; i++ )
         if ( strings[ i ].startsWith( "st" ) )
            output += "\"" + strings[ i ] +
                      "\" starts with \"st\"\n";

      output += "\n";

      // Test method startsWith starting from position
      // 2 of the string
      for ( int i = 0; i < strings.length; i++ )
         if ( strings[ i ].startsWith( "art", 2 ) ) 
            output += 
               "\"" + strings[ i ] +
               "\" starts with \"art\" at position 2\n";

      output += "\n";

      // Test method endsWith
      for ( int i = 0; i < strings.length; i++ )
         if ( strings[ i ].endsWith( "ed" ) )
            output += "\"" + strings[ i ] +
                      "\" ends with \"ed\"\n";

      JOptionPane.showMessageDialog( null, output,
         "Demonstrating String Class Comparisons",
         JOptionPane.INFORMATION_MESSAGE );

      System.exit( 0 );
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?