... "started", "starting", "ended", "ending"};
 7
 8         // test method startsWith
 9         for (String string : strings) {
10            if (string.startsWith("st")) {
11               System.out.printf("\"%s\" starts with \"st\"%n", string);
12            }
13         }
14
15         System.out.println();
16
17         // test method startsWith starting from position 2 of string
18         for (String string : strings) {
19            if (string.startsWith("art", 2)) {
20               System.out.printf(
21                  "\"%s\" starts with \"art\" at position 2%n", string);
22            }
23         }
24
25         System.out.println();
26
27         // test method endsWith
28         for (String string : strings) {
29            if (string.endsWith("ed")) {
30               System.out.printf("\"%s\" ends with \"ed\"%n", string);
31            }
32         }
33      }
34   }
 "started" starts with "st" "starting" starts with "st" "started" starts with "art" at ...

Get Java How To Program, Late Objects, 11th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.