1 package net.sf.mmapps.commons.util;
2
3 /***
4 * @author Nico Klasens
5 */
6 public class StringUtil {
7
8 /***
9 * is Empty.String
10 *
11 * @param str String to check emptiness
12 * @return boolean is it empty
13 */
14 public static boolean isEmpty(String str) {
15 return (str == null) || "".equals(str);
16 }
17
18 /***
19 * is Empty Or Whitespace.String
20 *
21 * @param str String to check emptiness
22 * @return boolean is it empty
23 */
24 public static boolean isEmptyOrWhitespace(String str) {
25 return (str == null) || "".equals(str.trim());
26 }
27
28 /***
29 * Add Leading Char.to the string
30 *
31 * @param str String to add character in front of
32 * @param ch character to add
33 * @param total total length of output string
34 * @return String with leading characters
35 */
36 public static String addLeadingChar(String str, char ch, int total) {
37 String result = str;
38
39 while (result.length() < total) {
40 result = ch + result;
41 }
42
43 return result;
44 }
45
46 /***
47 * Transforms a string by replacing every occurence of a given searchstring with another string.
48 * <br>
49 * If the string or searchstring is empty then the string is returned without modifictions. The
50 * searchstring occurences are deleted from the string if the replace string is empty Example :<br>
51 * <ul>
52 * replace("12345","34","678") gives "126785".
53 * </ul>
54 *
55 * @param str The sourcestring.
56 * @param oldToken The searchstring.
57 * @param newToken The replacestring.
58 * @return The transformed string.
59 */
60 public static String replace(String str, String oldToken, String newToken) {
61 if (isEmpty(str) || isEmpty(oldToken)) {
62
63
64 return str;
65 }
66
67
68 if (newToken == null) {
69 newToken = "";
70 }
71
72 StringBuffer result = new StringBuffer("");
73 int pos = str.indexOf(oldToken);
74 int startOfIndex = 0;
75 while (pos != -1) {
76 result.append(str.substring(startOfIndex, pos));
77 result.append(newToken);
78 startOfIndex = pos + oldToken.length();
79 pos = str.indexOf(oldToken, startOfIndex);
80 }
81
82 result.append(str.substring(startOfIndex));
83 return result.toString();
84 }
85
86 /***
87 * Transforms a string by replacing every occurence of a given searchstring with another string.
88 * <br>
89 * If the string or searchstring is empty then the string is returned without modifictions. The
90 * searchstring occurences are deleted from the string if the replace string is empty Example :<br>
91 * <ul>
92 * replace("12345","34","678") gives "126785".
93 * </ul>
94 *
95 * @param str The sourcestring.
96 * @param oldToken The searchstring.
97 * @param newToken The replacestring.
98 * @return The transformed string.
99 */
100 public static String replaceIgnoreCase(String str, String oldToken, String newToken) {
101 if (isEmpty(str) || isEmpty(oldToken)) {
102
103
104 return str;
105 }
106
107 if (newToken == null) {
108 newToken = "";
109 }
110
111 StringBuffer result = new StringBuffer(str.length() + 100);
112 String strLower = str.toLowerCase();
113 String oldTokenLower = oldToken.toLowerCase();
114
115 int i = strLower.indexOf(oldTokenLower);
116 int startOfIndex = 0;
117 while (i != -1) {
118 result.append(str.substring(startOfIndex, i));
119 result.append(newToken);
120 startOfIndex = i + oldToken.length();
121 i = strLower.indexOf(oldTokenLower, startOfIndex);
122 }
123
124 result.append(str.substring(startOfIndex));
125 return result.toString();
126 }
127
128 /***
129 * This method counts the occurences of 'needle' in the string 'haystack'.
130 *
131 * @param haystack full string
132 * @param needle needle to search for
133 * @return number of occurences
134 */
135 public static int countOccurences(String haystack, String needle) {
136 return countOccurences(haystack, needle, 0);
137 }
138
139 /***
140 * This method counts the occurences of 'needle' in the string 'haystack' starting from position
141 * 'position'.
142 *
143 * @param haystack full string
144 * @param needle needle to search for
145 * @param position starting position
146 * @return number of occurences
147 */
148 public static int countOccurences(String haystack, String needle, int position) {
149 int count = 0;
150
151 if ((haystack != null) && (needle != null)) {
152 position = haystack.indexOf(needle, position);
153
154 while (position >= 0) {
155 count++;
156 position = haystack.indexOf(needle, position + 1);
157 }
158 }
159
160 return count;
161 }
162
163 public static String escapeXml(String str) {
164 str = str.replaceAll("&", "&");
165 str = str.replaceAll("<", "<");
166 str = str.replaceAll(">", ">");
167 str = str.replaceAll("\"", """);
168 str = str.replaceAll("'", "'");
169 return str;
170 }
171
172 /***
173 * Generates a String with length 'len', filled with random(ish) characters from 'begin' to
174 * 'end'. e.g. randomString(3, 'a', 'm') can return "kgm", "faa", "dlb", etc. but never "kgn"
175 * (because 'n' doesn't lie between 'a' and 'm')
176 *
177 * @param len the length of the desired string
178 * @param begin the first valid character in the sequence of possible characters
179 * @param end the first valid character in the sequence of possible characters
180 * @return the random(ish) string, as specified by the above parameters
181 */
182 public static String randomString(int len, char begin, char end) {
183 if (end < begin) {
184 char temp = begin;
185 begin = end;
186 end = temp;
187 }
188
189 String temp = "";
190 for (int i = 0; i < len; i++) {
191 temp += "" + (char) (begin + (int) (Math.random() * (end - begin + 1)));
192 }
193 return temp;
194 }
195
196 /***
197 * Wraps the given string, using the java.text.BreakIterator.
198 * @param str The string that might be too long
199 * @param lineLength The maximum length of the lines to be returned.
200 * @return wrapped string
201 */
202 public static String wrap(String str, int lineLength) {
203 StringBuffer sb = new StringBuffer();
204
205 java.text.BreakIterator iterator = java.text.BreakIterator.getWordInstance();
206 iterator.setText(str);
207 int index;
208 int oldIndex = 0;
209 int lineStart = 0;
210 while ((index = iterator.next()) != java.text.BreakIterator.DONE) {
211 if (index - lineStart > lineLength) {
212 sb.append(str.substring(lineStart, oldIndex));
213
214
215 if (str.charAt(oldIndex) == '.' || str.charAt(oldIndex) == ',') {
216 sb.append(str.charAt(oldIndex));
217 oldIndex++;
218 }
219 sb.append('\n');
220
221
222 if (str.charAt(oldIndex) == ' ') {
223 oldIndex++;
224 }
225 lineStart = oldIndex;
226 }
227 oldIndex = index;
228 }
229
230 sb.append(str.substring(lineStart));
231
232 return sb.toString();
233 }
234
235 /***
236 * Returns a new string with the given maximum length of the given string, with "..." appended
237 * to the end if the maximum length was exceeded.
238 *
239 * @param str The string that might be too long
240 * @param maxLength The maximum length of the string to be returned, ignored if <= 0.
241 * @return truncated string
242 */
243 public static String truncate(String str, int maxLength) {
244 if (str == null) return null;
245 if (maxLength > 0) {
246 if (str.length() > maxLength) {
247 return str.substring(0, Math.max(0, maxLength - 3)) + "...";
248 }
249 }
250 return str;
251 }
252
253
254 /***
255 * Remove all whitespaces from a string
256 *
257 * @param s String which might contain whitespaces
258 * @return String which contains no whitespaces any more
259 */
260 public static String removeWhitespaces(String s) {
261 if (s == null) s = "";
262
263 StringBuffer sbNew = new StringBuffer();
264 for (int i = 0; i < s.length(); i++) {
265 if (!Character.isSpaceChar(s.charAt(i))) {
266 sbNew.append(s.charAt(i));
267 }
268 }
269 return sbNew.toString();
270 }
271
272 }