1 | |
package org.jtheque.utils; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
import java.util.Locale; |
20 | |
import java.util.regex.Pattern; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public final class StringUtils { |
28 | |
public static final String EMPTY_STRING = ""; |
29 | |
|
30 | 2 | private static final Pattern NUMBER_PATTERN = Pattern.compile("[0-9]"); |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
private StringUtils() { |
36 | 0 | super(); |
37 | 0 | } |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public static String setFirstLetterOnlyUpper(String word) { |
46 | 12 | if (isEmpty(word)) { |
47 | 4 | return word; |
48 | |
} |
49 | |
|
50 | 8 | char[] chars = word.toLowerCase(Locale.getDefault()).toCharArray(); |
51 | |
|
52 | 8 | chars[0] = Character.toUpperCase(chars[0]); |
53 | |
|
54 | 8 | return String.valueOf(chars); |
55 | |
} |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public static String setFirstLetterUpper(String word) { |
64 | 24 | if (isEmpty(word)) { |
65 | 4 | return word; |
66 | |
} |
67 | |
|
68 | 20 | char[] chars = word.toCharArray(); |
69 | |
|
70 | 20 | chars[0] = Character.toUpperCase(chars[0]); |
71 | |
|
72 | 20 | return String.valueOf(chars); |
73 | |
} |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public static boolean isEmpty(CharSequence string) { |
82 | 300 | return string == null || string.length() == 0; |
83 | |
} |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public static boolean isNotEmpty(CharSequence string) { |
92 | 42 | return !isEmpty(string); |
93 | |
} |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public static void removeLastSpace(StringBuilder builder) { |
101 | 6 | if (builder.length() > 0) { |
102 | 4 | builder.replace(builder.length() - 1, builder.length(), EMPTY_STRING); |
103 | |
} |
104 | 6 | } |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public static String removeHTMLEntities(String htmlString) { |
113 | 12 | String cleaned = htmlString.replaceAll("'", "'"); |
114 | |
|
115 | 12 | cleaned = cleaned.replaceAll("<br[ ]?[/]?>", "\n"); |
116 | |
|
117 | 12 | return cleaned; |
118 | |
} |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public static boolean isStringSurroundedWith(CharSequence str, char c) { |
128 | 10 | if (isEmpty(str)) { |
129 | 2 | return false; |
130 | |
} |
131 | |
|
132 | 8 | return str.charAt(0) == c && str.charAt(str.length() - 1) == c; |
133 | |
} |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public static String removeSurroundedChars(String str) { |
142 | 12 | if (isEmpty(str)) { |
143 | 2 | return str; |
144 | |
} |
145 | |
|
146 | 10 | return str.substring(1, str.length() - 1); |
147 | |
} |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
@SuppressWarnings({"DynamicRegexReplaceableByCompiledPattern"}) |
156 | |
public static String removeUnicode(String value) { |
157 | 0 | String refreshed = value.replace("\\\\u00e0", "\u00e0"); |
158 | 0 | refreshed = refreshed.replace("\\\\u00e2", "\u00e2"); |
159 | 0 | refreshed = refreshed.replace("\\\\u00e4", "\u00e4"); |
160 | 0 | refreshed = refreshed.replace("\\\\u00e7", "\u00e7"); |
161 | 0 | refreshed = refreshed.replace("\\\\u00e8", "\u00e8"); |
162 | 0 | refreshed = refreshed.replace("\\\\u00e9", "\u00e9"); |
163 | 0 | refreshed = refreshed.replace("\\\\u00ea", "\u00ea"); |
164 | 0 | refreshed = refreshed.replace("\\\\u00eb", "\u00eb"); |
165 | 0 | refreshed = refreshed.replace("\\\\u00ee", "\u00ee"); |
166 | 0 | refreshed = refreshed.replace("\\\\u00ef", "\u00ef"); |
167 | 0 | refreshed = refreshed.replace("\\\\u00f4", "\u00f4"); |
168 | 0 | refreshed = refreshed.replace("\\\\u00f6", "\u00f6"); |
169 | 0 | refreshed = refreshed.replace("\\\\u00f9", "\u00f9"); |
170 | 0 | refreshed = refreshed.replace("\\\\u00fb", "\u00fb"); |
171 | 0 | refreshed = refreshed.replace("\\\\u00fc", "\u00fc"); |
172 | |
|
173 | 0 | return refreshed; |
174 | |
} |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
public static boolean endsWithOneOf(String value, String... ends) { |
184 | 10 | for (String end : ends) { |
185 | 8 | if (value.endsWith(end)) { |
186 | 2 | return true; |
187 | |
} |
188 | |
} |
189 | |
|
190 | 2 | return false; |
191 | |
} |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
public static boolean isEmpty(String[] strings) { |
204 | 24 | if (strings != null && strings.length > 0) { |
205 | 44 | for (String s : strings) { |
206 | 36 | if (isNotEmpty(s)) { |
207 | 8 | return false; |
208 | |
} |
209 | |
} |
210 | |
} |
211 | |
|
212 | 16 | return true; |
213 | |
} |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
public static boolean isNotEmpty(String[] strings) { |
223 | 12 | return !isEmpty(strings); |
224 | |
} |
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
public static String removeNumbers(CharSequence value) { |
233 | 4 | return NUMBER_PATTERN.matcher(value).replaceAll(""); |
234 | |
} |
235 | |
|
236 | |
public static String delete(String string, Object... toDeletes){ |
237 | 0 | String cleared = string; |
238 | |
|
239 | 0 | for(Object toDelete : toDeletes){ |
240 | 0 | cleared = delete(cleared, toDelete.toString()); |
241 | |
} |
242 | |
|
243 | 0 | return cleared; |
244 | |
} |
245 | |
|
246 | |
public static String delete(String string, CharSequence toDelete){ |
247 | 0 | return string.replace(toDelete, ""); |
248 | |
} |
249 | |
} |