Source code: javatools/util/StringUtils.java
1 /*
2 * StringUtils.java
3 *
4 * Created on 28 giugno 2002, 10.59
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package javatools.util;
26
27 import java.util.*;
28
29 /**
30 * Contains a set of function to manage string.
31 * @author Antonio Petrelli
32 * @version 0.2.1
33 */
34 public class StringUtils {
35
36 /** Creates new StringUtils */
37 public StringUtils() {
38 }
39
40 /** Sets as upper case the first letter of each word (and lowers all the others).
41 * @param inString The string to "properize".
42 * @return The "properized" string.
43 */
44 public static String proper(String inString) {
45 int i, numChars, status;
46 char[] inArray;
47 char curChar;
48
49 inArray = inString.toCharArray();
50 numChars = inArray.length;
51 status = SPACE_STATUS;
52 for (i=0; i < numChars; i++) {
53 curChar = inArray[i];
54 switch (status) {
55 case NORMAL_STATUS:
56 inArray[i] = Character.toLowerCase(curChar);
57 if (!Character.isLetterOrDigit(curChar) && curChar != '\'')
58 status = SPACE_STATUS;
59 break;
60 case SPACE_STATUS:
61 inArray[i] = Character.toUpperCase(curChar);
62 if (Character.isLetterOrDigit(curChar))
63 status = NORMAL_STATUS;
64 }
65 }
66 return new String(inArray);
67 }
68
69 /** Removes extra spaces, replaces spaces with "%" and puts "%" at the beginning and
70 * at the end of the passed string. Useful in "LIKE" clauses.
71 * @param inString The string to be processed.
72 * @return The final string.
73 */
74 public static String toSQLSearchString(String inString) {
75 int i, numChars, status;
76 char curChar;
77 String tempString;
78 StringBuffer strbuf;
79
80 strbuf = new java.lang.StringBuffer();
81 tempString = inString.trim();
82 numChars = tempString.length();
83 status = NORMAL_STATUS;
84 for (i=0; i < numChars; i++) {
85 curChar = tempString.charAt(i);
86 switch (status) {
87 case NORMAL_STATUS:
88 if (curChar != ' ')
89 strbuf.append(curChar);
90 else {
91 strbuf.append('%');
92 status = SPACE_STATUS;
93 }
94 case SPACE_STATUS:
95 if (curChar != ' ') {
96 strbuf.append(curChar);
97 status = NORMAL_STATUS;
98 }
99 }
100 }
101 if (numChars >= 0)
102 return "%" + strbuf.toString() + "%";
103 else
104 return null;
105 }
106
107 /** Tokenizes a string (using spaces as delimiters) and returns the tokens as an
108 * array.
109 * @param inString The string to be processed.
110 * @return The token array.
111 */
112 public static String[] toTokenArray(String inString) {
113 List tempList;
114 String[] tempArray;
115
116 tempList = toTokenList(inString);
117 tempArray = new String[tempList.size()];
118 tempList.toArray(tempArray);
119 return tempArray;
120 }
121
122 /** Tokenizes a string (using a specified delimiter) and returns the tokens as an
123 * array.
124 * @param inString The string to be processed.
125 * @param delim The delimiter to be used.
126 * @return The token array.
127 */
128 public static String[] toTokenArray(String inString, String delim) {
129 List tempList;
130 String[] tempArray;
131
132 tempList = toTokenList(inString, delim);
133 tempArray = new String[tempList.size()];
134 tempList.toArray(tempArray);
135 return tempArray;
136 }
137
138 /** Tokenizes a string (using spaces as delimiters) and returns the tokens as a list.
139 * @param inString The string to be processed.
140 * @return The token list.
141 */
142 public static List toTokenList(String inString) {
143 StringTokenizer st;
144 LinkedList tempList;
145
146 st = new StringTokenizer(inString);
147 tempList = new LinkedList();
148 while (st.hasMoreTokens())
149 tempList.add(st.nextToken());
150 return tempList;
151 }
152
153 /** Tokenizes a string (using a specified delimiter) and returns the tokens as
154 * a List.
155 * @param inString The string to be processed.
156 * @param delim The delimiter to be used.
157 * @return The token list.
158 */
159 public static List toTokenList (String inString, String delim) {
160 StringTokenizer st;
161 LinkedList tempList;
162 String tempToken;
163
164 st = new StringTokenizer(inString, delim);
165 tempList = new LinkedList();
166 while (st.hasMoreTokens()) {
167 tempToken = st.nextToken();
168 if (!tempToken.equals(""))
169 tempList.add(tempToken);
170 }
171 return tempList;
172 }
173
174 /** Tokenizes a string (using spaces as delimiters) and returns the tokens as a list.
175 * @param inString The string to be processed.
176 * @return The token list.
177 */
178 public static Set toTokenSet(String inString) {
179 StringTokenizer st;
180 HashSet tempSet;
181
182 st = new StringTokenizer(inString);
183 tempSet = new HashSet();
184 while (st.hasMoreTokens())
185 tempSet.add(st.nextToken());
186 return tempSet;
187 }
188
189 /** Tokenizes a string (using a specified delimiter) and returns the tokens as
190 * a List.
191 * @param inString The string to be processed.
192 * @param delim The delimiter to be used.
193 * @return The token list.
194 */
195 public static Set toTokenSet(String inString, String delim) {
196 StringTokenizer st;
197 HashSet tempSet;
198 String tempToken;
199
200 st = new StringTokenizer(inString, delim);
201 tempSet = new HashSet();
202 while (st.hasMoreTokens()) {
203 tempToken = st.nextToken();
204 if (!tempToken.equals(""))
205 tempSet.add(tempToken);
206 }
207 return tempSet;
208 }
209
210 private static final int NORMAL_STATUS = 0;
211 private static final int SPACE_STATUS = 1;
212
213 }