import objectdraw.*; /** * Simple examples of working with Strings. */ public class StringBasics extends FrameWindowController { public void begin() { String str = "this is a string"; int len = str.length(); char firstChar = str.charAt(0); char lastChar = str.charAt(str.length() - 1); } // Get the letter following c. Wrap around from 'z' to 'a'. public char nextLetter(char c) { if (c == 'z') { return 'a'; } else { // Note: (c + 1) converts to an int, // so need to explicitly convert back to a char by // adding (char) -- this is called a cast. return (char) (c + 1); } } // Get whether c is a digit. public boolean isDigit(char c) { return (c >= '0' && c <= '9'); } // Get the first name out of a "firstname lastname" string. public String getFirstName(String fullName) { return fullName.substring(0, fullName.indexOf(" ")); } // Get the last name out of a "firstname lastname" string. public String getLastName(String fullName) { return fullName.substring(fullName.indexOf(" ") + 1); } }