import java.util.Scanner; /** Some more examples for String processing. */ public class StringSearching { /** String Searching methods */ public static void main(String[] args) { String doc="Four score and seven years ago"; // use a loop to find all locations of the letter 'r' int loc = doc.indexOf('r'); while (loc!=-1) { System.out.println("r was found at location: " + loc); loc = doc.indexOf('r', loc+1); } // search for a pattern in a text String text = "I am searching for a needle in a haystack."; String pattern = "in"; loc = text.indexOf(pattern); while (loc!=-1) { System.out.println(pattern + " occurs at location " + loc); loc = text.indexOf(pattern, loc+1); } // Problem: You have a string with a last name then ',' comma // first name. You want to extract the initials into // character variables, firstInit and lastInit String fullName="Doe,John"; char firstInit, lastInit; lastInit = fullName.charAt(0); firstInit=fullName.charAt(fullName.indexOf(',')+1); System.out.println("Your initials are: " + firstInit + lastInit); } } /** LAB ASSIGNMENT: * Write a method that accepts 2 String object as parameters. * The first is a pattern to search for, the second is a text to * search. * The method returns the number of times the pattern occurs * in the text. */