import java.awt.*; import java.awt.event.*; import StringModifier; /** * StringModifiers that know how to extract the first word from a string. * * @author Samuel A. Rebelsky * @version 1.0 of February 1999 */ public class FirstWord extends StringModifier { /** * Given a string, return the first word in the string. If there's only * one word in the string, returns that word. */ public String modify(String str) { // Determine where (if anywhere) the first space falls. int spacePos = str.indexOf(" "); // If the space is in the string, extract everything up to // the space. if (spacePos > 0) { return str.substring(0,spacePos); } // Otherwise, just keep the string as is else { return str; } } // modify(String) } // class FirstWord