001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: StringUtils.java,v $ 023 Revision 1.8 2004/05/12 18:03:42 markl 024 javadoc updates 025 026 Revision 1.7 2004/05/05 21:22:45 markl 027 Comment header updates. 028 029 Revision 1.6 2004/03/10 00:50:07 markl 030 added wordBreak() method 031 032 Revision 1.5 2003/01/19 09:42:39 markl 033 Javadoc & comment header updates. 034 035 Revision 1.4 2001/03/12 03:16:51 markl 036 *** empty log message *** 037 038 Revision 1.3 1999/06/08 06:46:33 markl 039 Added getClassName() method. 040 041 Revision 1.2 1999/01/10 03:56:22 markl 042 added GPL header & RCS tag 043 ---------------------------------------------------------------------------- 044*/ 045 046package kiwi.util; 047 048import java.util.*; 049import java.io.*; 050 051/** A class of miscellaneous string utilities. All of the methods in this class 052 * are static. 053 * 054 * @author Mark Lindner 055 */ 056 057public final class StringUtils 058 { 059 060 private StringUtils() { } 061 062 /** Left justify a string, wrapping words as necessary. 063 * 064 * @param text The text to justify. 065 * @param cols The number of columns to use. 066 */ 067 068 public final static String justify(String text, int cols) 069 { 070 StringTokenizer st = new StringTokenizer(text, "\t \f\n", true); 071 StringBuffer buf = new StringBuffer(500); 072 int ww, lw = 0; 073 boolean sawLF = false, first = true; 074 075 while(st.hasMoreTokens()) 076 { 077 String tok = st.nextToken(); 078 079 if(tok.equals("\n")) 080 { 081 if(cols == 0) continue; 082 083 if(sawLF) 084 { 085 buf.append('\n'); 086 buf.append('\n'); 087 lw = 0; 088 first = true; 089 sawLF = false; 090 } 091 else sawLF = true; 092 } 093 094 else if((tok.equals(" ")) || (tok.equals("\t")) || (tok.equals("\f"))) 095 { 096 sawLF = false; 097 continue; 098 } 099 else 100 { 101 sawLF = false; 102 ww = tok.length(); 103 if(!first) ww++; 104 if((lw + ww) > cols) 105 { 106 buf.append('\n'); 107 first = true; 108 lw = 0; 109 } 110 111 if(!first) buf.append(' '); 112 buf.append(tok); 113 lw += ww; 114 first = false; 115 } 116 } 117 118 String r = buf.toString(); 119 buf = null; 120 return(r); 121 } 122 123 /** Determine if a string consists solely of alphanumeric characters. 124 * 125 * @param text The string to test. 126 * 127 * @return <code>true</code> if the string contains only alphanumeric 128 * characters, and <code>false</code> otherwise. 129 */ 130 131 public final static boolean isAlphaNumeric(String text) 132 { 133 for(int i = 0; i < text.length(); i++) 134 if(!Character.isLetterOrDigit(text.charAt(i))) 135 return(false); 136 137 return(true); 138 } 139 140 /** Split a string into a series of tokens based on the given delimiter. 141 * 142 * @param s The string to split. 143 * @param delimiter A string consisting of characters that should be treated 144 * as delimiters. 145 * @return An array of tokens. 146 * @see #join 147 */ 148 149 public final static String[] split(String s, String delimiter) 150 { 151 Vector v = new Vector(); 152 StringTokenizer st = new StringTokenizer(s, delimiter); 153 while(st.hasMoreTokens()) 154 v.addElement(st.nextToken()); 155 156 String array[] = new String[v.size()]; 157 v.copyInto(array); 158 159 return(array); 160 } 161 162 /** Join an array of strings into a single string of tokens using the given 163 * delimiter. 164 * 165 * @param array The tokens to join. 166 * @param delimiter A string to insert between adjacent tokens. 167 * @return The resulting string. 168 * @see #split 169 */ 170 171 public final static String join(String array[], String delimiter) 172 { 173 StringBuffer sb = new StringBuffer(); 174 for(int i = 0; i < array.length; i++) 175 { 176 if(i > 0) sb.append(delimiter); 177 sb.append(array[i]); 178 } 179 180 return(sb.toString()); 181 } 182 183 /** Break a string into whitespace delimited words, treating substrings 184 * enclosed within pairs of quotes as single words. 185 * 186 * @param text The text to parse. 187 * @param quoteChar The quote character, typically the single- or 188 * double-quote. 189 * @return A vector of the parsed words. 190 * 191 * @since Kiwi 2.0 192 */ 193 194 public final static String[] wordBreak(String text, int quoteChar) 195 { 196 if(text == null) 197 return(null); 198 199 Vector vec = new Vector(); 200 201 try 202 { 203 StreamTokenizer st = new StreamTokenizer(new StringReader(text)); 204 st.ordinaryChars('!', '~'); 205 st.wordChars('!', '~'); 206 st.quoteChar(quoteChar); 207 208 while(st.nextToken() != st.TT_EOF) 209 { 210 if((st.ttype == st.TT_WORD) || (st.ttype == quoteChar)) 211 vec.addElement(st.sval); 212 } 213 } 214 catch(IOException ex) 215 { 216 } 217 218 String words[] = new String[vec.size()]; 219 vec.copyInto(words); 220 221 return(words); 222 } 223 224 /** Get the name of a class; this method returns the last component of the 225 * fully qualified name of the given class. For example, 'String' is returned 226 * for the class <b>java.lang.String</b>. 227 * 228 * @param clazz The class. 229 * @return The name of the class. 230 */ 231 232 public final static String getClassName(Class clazz) 233 { 234 String s = clazz.getName(); 235 236 int idx = s.lastIndexOf('.'); 237 238 return(idx < 0 ? s : s.substring(++idx)); 239 } 240 241 } 242 243/* end of source file */