/** 12/3/2018 * substring * concat (same as +) s1+s2 s1.concat(s2) * trim * replace, replaceAll, split we also discussed regular expressions, specifically use [ ] to hold a set of characters to match (both split and replaceAll accept a regular expresssion.*/ import java.util.Scanner; public class MoreStringMethods { public static void main(String[] args) { String name = "John Doe"; String first = name.substring(0,5); String last = name.substring(5); first = first.trim(); System.out.println("first name is: " + first + " last name: " + last); // the above ex (John Doe) was trivial since we used a specific name // suppose I want to extract first and last name in general System.out.println("Enter first name followed by last name (separated with a space): "); Scanner sc = new Scanner(System.in); name = sc.nextLine(); // read in String with spaces int loc = name.indexOf(" "); System.out.println("loc is: " + loc); if (loc!=-1) { first = name.substring(0, loc); last = name.substring(loc+1); System.out.println("first name is: " + first + " last name: " + last); } // print the initials (assuming I have fullname in name) char firstInit = name.charAt(0); char lastInit = name.charAt(name.indexOf(" ")+1); System.out.println("Initials are:"); System.out.println(firstInit + " " + lastInit); String sentence = "Today is Monday, next week is the last week of the semester."; int n=3; // retreive the nth word from the sentence // Solution #1 // find space n-1 loc=-1; for (int i=0; i