001 /* 002 * cs101 String utilities 003 * $Id: StringUtils.java,v 1.1.1.1 2002/06/05 21:56:32 root Exp $ 004 * 005 * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group. 006 * For more information, see <a href="http://www.ai.mit.edu/projects/cs101/">the 007 * CS101 homepage</a> or email <las@ai.mit.edu>. 008 * 009 * Copyright (C) 1996 Massachusetts Institute of Technology. 010 * Please do not redistribute without obtaining permission. 011 */ 012 013 package cs101.util; 014 015 /** 016 * cs101.util.StringUtils implements some string utility functions. 017 * It is intended as a library class, i.e., all methods are static. 018 * <br> 019 * Copyright 1996 Massachusetts Institute of Technology 020 * 021 * @author Lynn Andrea Stein, las@ai.mit.edu 022 * @version $Id: StringUtils.java,v 1.1.1.1 2002/06/05 21:56:32 root Exp $ 023 * 024 */ 025 public final class StringUtils { 026 /** 027 * A string containing the non-word characters a user might 028 * reasonably be expected to type at a semi-standard American 029 * keyboard. Suitable for using as delimiters for 030 * java.util.StringTokenizer. 031 * 032 * @see java.util.StringTokenizer 033 */ 034 public static final String nonWordChars 035 = "!@#$%^&*()_-+=|`~<>,.?/:;\"\' \t\n\b\f\r{}[]\\"; 036 037 /** 038 * Determines whether its argument is a word or a delimiter as 039 * defined by a java.util.StringTokenizer when initialized with 040 * StringUtils.nonWordChars as a delimiter string. 041 * 042 * @param word String to be tested for "wordness". 043 * 044 * @return false if word is one of the delimiters in 045 * StringUtils.nonWordChars, true otherwise. 046 * 047 * @see java.util.StringTokenizer 048 * @see #nonWordChars 049 * @see java.lang.String 050 */ 051 public static final boolean wordP (String word) { 052 return ( (! (word.length() == 1)) 053 || (StringUtils.nonWordChars.indexOf(word.charAt(0)) == -1 )); 054 } 055 056 /** 057 * Determines whether the first character of its argument word is 058 * upper case. 059 * 060 * @param word String to be checked for capitalization. 061 * 062 * @return true if first character is upper case. 063 * 064 * @see java.lang.Character#isUpperCase 065 * @see java.lang.String#charAt 066 */ 067 public static final boolean capitalizedP (String word) { 068 return Character.isUpperCase(word.charAt(0)); 069 } 070 071 072 /** 073 * Determines whether all of the characters in its argument word are 074 * upper case. 075 * 076 * @param word String to be checked for capitalization. 077 * 078 * @return true if all characters are is upper case. 079 * 080 * @see java.lang.Character#isLowerCase 081 * @see java.lang.String#charAt 082 * @see java.lang.String#length 083 */ 084 public static final boolean allUpperCaseP (String word) { 085 for (int i = 0; i < word.length(); i++) { 086 if (Character.isLowerCase(word.charAt(i))) { 087 return false; 088 } 089 } 090 return true; 091 } 092 093 /** 094 * Constructs a new string identical to its argument, but with the 095 * first character replaced by its upper case equivalent. 096 * 097 * @param word String to be capitalized. 098 * 099 * @return the new, capitalized String. 100 * 101 * @see java.lang.Character#toUpperCase 102 * @see java.lang.StringBuffer 103 * @see java.lang.String#charAt 104 * @see java.lang.String#valueOf 105 */ 106 public static final String capitalize (String word) { 107 if (word.length() == 1) { 108 return ( String.valueOf(Character.toUpperCase(word.charAt(0))) ); 109 } else { 110 return ( String.valueOf(Character.toUpperCase(word.charAt(0))) 111 + word.substring(1) ); 112 } 113 } 114 115 /** 116 * Constructs a new string identical to its argument, but with all 117 * characters replaced by their upper case equivalents. 118 * 119 * @param word String to be uppercased. 120 * 121 * @return the new, uppercased String. 122 * 123 * @see java.lang.Character#toUpperCase 124 * @see java.lang.String#length 125 * @see java.lang.String#charAt 126 * @see java.lang.StringBuffer 127 */ 128 public static final String capitalizeAll (String word) { 129 StringBuffer tmp = new StringBuffer (word.length()); 130 131 for (int i = 0; i < word.length(); i++) { 132 tmp.insert (i, Character.toUpperCase(word.charAt (i))); 133 } 134 return tmp.toString(); 135 } 136 137 /** 138 * Constructs a new string identical to its argument, but with the 139 * first character replaced by its lower case equivalent. 140 * 141 * @param word String to be unCapitalized. 142 * 143 * @return the new, unCapitalized String. 144 * 145 * @see java.lang.Character#toLowerCase 146 * @see java.lang.String#length 147 * @see java.lang.String#charAt 148 * @see java.lang.String#valueOf 149 */ 150 public static final String unCapitalize (String word) { 151 if (word.length() == 1) { 152 return ( String.valueOf(Character.toLowerCase(word.charAt(0))) ); 153 } else { 154 return ( String.valueOf(Character.toLowerCase(word.charAt(0))) 155 + word.substring(1) ); 156 } 157 } 158 159 /** 160 * Computes the index of the first position in String which 161 * contains a vowel (i.e., one of AEIOU). 162 * 163 * @param word String to be searched for vowel. 164 * 165 * @return index of first vowel, length of word if no vowel. 166 * 167 * @see java.lang.String#length 168 * @see java.lang.String#charAt 169 */ 170 public static final int firstVowelPos ( String word ) { 171 int i = 0, length = word.length(); 172 173 LETTER: 174 for(i = 0; i < length; i++) { 175 switch(word.charAt(i)) { 176 case 'a': case 'A': 177 case 'e': case 'E': 178 case 'i': case 'I': 179 case 'o': case 'O': 180 case 'u': case 'U': break LETTER; 181 default: 182 } 183 } 184 if (i == length) { 185 return 0; 186 } else { 187 return i; 188 } 189 190 } 191 192 /** 193 * Prevent instantiation 194 */ 195 private StringUtils() {} 196 } 197 198 /* Comments: 199 * 200 * History: 201 * $Log: StringUtils.java,v $ 202 * Revision 1.1.1.1 2002/06/05 21:56:32 root 203 * CS101 comes to Olin finally. 204 * 205 * Revision 1.3 1998/07/24 17:19:35 tparnell 206 * Placate new javadoc behavior 207 * 208 * Revision 1.2 1998/07/21 19:44:07 tparnell 209 * added private StringUtils() 210 * 211 * Revision 1.1 1998/03/13 22:18:23 tparnell 212 * Import from server crash. I think the src and class files match up. 213 * 214 * Revision 1.4 1996/08/01 18:26:33 reuben 215 * More javadoc tweaking (hopefully the final pass) 216 * 217 * Revision 1.3 1996/08/01 16:23:30 reuben 218 * Fixed javadoc problem. 219 * 220 * Revision 1.2 1996/07/30 17:26:01 reuben 221 * Added/corrected javadoc comments. 222 * 223 * Revision 1.1.1.1 1996/07/18 17:38:24 sit 224 * Import from /mit/6.096/share/classes after 6.80s session 225 * 226 * Revision 1.3 1996/06/19 23:04:57 las 227 * Cleanerer. 228 * 229 * Revision 1.2 1996/06/19 22:48:05 las 230 * Cleaned up documentation. 231 * 232 * 6-19-96 Documentation cleaned up by las@ai.mit.edu 233 * 6-18-96 Created by las@ai.mit.edu 234 * 235 * 236 */ 237 238 239 240 241