14.3.4 Locating Characters and Substrings in Strings

Often it’s useful to search a string for a character or set of characters. For example, if you’re creating your own word processor, you might want to provide a capability for searching through documents. Figure 14.5 demonstrates the many versions of String methods indexOf and lastIndexOf that search for a specified character or substring in a String.

 1   // Fig. 14.5: StringIndexMethods.java 2   // String searching methods indexOf and lastIndexOf. 3  4   public class StringIndexMethods 5   { 6      public static void main(String[] args) 7      { 8         String letters = "abcdefghijklmabcdefghijklm"9 10         // test indexOf to locate a character in a string ...

Get Java™ How To Program (Early Objects), Tenth 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.