提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
字符串方法startsWith和endsWith。
public class Main { public static void main(String[] args) /* n o w j a v a . c o m - 时 代 Java */ { String[] strings = {"started", "starting", "ended", "ending"}; // test method startsWith for (String string : strings) { if (string.startsWith("st")) System.out.printf("\"%s\" starts with \"st\"\n", string); } System.out.println(); // from 时 代 J a v a - N o w J a v a . c o m // test method startsWith starting from position 2 of string for (String string : strings) { if (string.startsWith("art", 2)) System.out.printf( "\"%s\" starts with \"art\" at position 2\n", string); } System.out.println(); // test method endsWith