svn-gvsig-desktop / tags / Root_v061 / applications / appCatalogYNomenclatorClient / src / es / gva / cit / catalogClient / utils / Strings.java @ 4812
History | View | Annotate | Download (7.29 KB)
1 | 3566 | jorpiell | |
---|---|---|---|
2 | 2820 | jorpiell | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
3 | *
|
||
4 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
19 | *
|
||
20 | * For more information, contact:
|
||
21 | *
|
||
22 | * Generalitat Valenciana
|
||
23 | * Conselleria d'Infraestructures i Transport
|
||
24 | * Av. Blasco Ib??ez, 50
|
||
25 | * 46010 VALENCIA
|
||
26 | * SPAIN
|
||
27 | *
|
||
28 | * +34 963862235
|
||
29 | * gvsig@gva.es
|
||
30 | * www.gvsig.gva.es
|
||
31 | *
|
||
32 | * or
|
||
33 | *
|
||
34 | * IVER T.I. S.A
|
||
35 | * Salamanca 50
|
||
36 | * 46005 Valencia
|
||
37 | * Spain
|
||
38 | *
|
||
39 | * +34 963163400
|
||
40 | * dac@iver.es
|
||
41 | */
|
||
42 | 2047 | luisw | package es.gva.cit.catalogClient.utils; |
43 | |||
44 | 3993 | jorpiell | import java.util.Vector; |
45 | |||
46 | 2047 | luisw | /**
|
47 | 3143 | jorpiell | * String Functions
|
48 | 3566 | jorpiell | *
|
49 | *
|
||
50 | 2820 | jorpiell | * @author Jorge Piera Llodra (piera_jor@gva.es)
|
51 | 3566 | jorpiell | */
|
52 | 2047 | luisw | public class Strings { |
53 | 3566 | jorpiell | |
54 | /**
|
||
55 | * Replace a part of a String
|
||
56 | *
|
||
57 | *
|
||
58 | * @return
|
||
59 | * @param str String to find the pattern
|
||
60 | * @param pattern Pattern to find
|
||
61 | * @param replace String to replace
|
||
62 | */
|
||
63 | public static String replace(String str, String pattern, String replace) { |
||
64 | 2047 | luisw | int s = 0; |
65 | int e = 0; |
||
66 | StringBuffer result = new StringBuffer(); |
||
67 | while ((e = str.indexOf(pattern, s)) >= 0) { |
||
68 | result.append(str.substring(s, e)); |
||
69 | result.append(replace); |
||
70 | 2724 | jorpiell | s = e + pattern.length(); |
71 | 2047 | luisw | } |
72 | result.append(str.substring(s)); |
||
73 | return result.toString();
|
||
74 | 3566 | jorpiell | } |
75 | 2724 | jorpiell | |
76 | 3566 | jorpiell | /**
|
77 | * Find a pattern into one array
|
||
78 | *
|
||
79 | *
|
||
80 | * @return
|
||
81 | * @param pattern
|
||
82 | * @param array
|
||
83 | */
|
||
84 | public static boolean find(String pattern, String[] array) { |
||
85 | 2724 | jorpiell | if (array != null) { |
86 | for (int i = 0; i < array.length; i++) |
||
87 | if (pattern.equals(array[i])) {
|
||
88 | return true; |
||
89 | } |
||
90 | } |
||
91 | return false; |
||
92 | 3566 | jorpiell | } |
93 | 2724 | jorpiell | |
94 | 3566 | jorpiell | /**
|
95 | * This function joins two strings
|
||
96 | *
|
||
97 | *
|
||
98 | * @return
|
||
99 | * @param s1
|
||
100 | * @param s2
|
||
101 | */
|
||
102 | public static String[] join(String[] s1, String[] s2) { |
||
103 | 2724 | jorpiell | if (s1 == null) { |
104 | return s2;
|
||
105 | } |
||
106 | if (s2 == null) { |
||
107 | return s1;
|
||
108 | } |
||
109 | String[] out = new String[s1.length + s2.length]; |
||
110 | for (int i = 0; i < s1.length; i++) |
||
111 | out[i] = s1[i]; |
||
112 | for (int i = 0; i < s2.length; i++) |
||
113 | out[s1.length + i] = s2[i]; |
||
114 | 3566 | jorpiell | return out;
|
115 | } |
||
116 | 2724 | jorpiell | |
117 | 3566 | jorpiell | /**
|
118 | * returns a list (comma-separated) in one unique string
|
||
119 | *
|
||
120 | *
|
||
121 | * @return
|
||
122 | * @param input
|
||
123 | */
|
||
124 | public static String getComaSeparated(String[] input) { |
||
125 | 3143 | jorpiell | return getXSeparated("," , input); |
126 | 3566 | jorpiell | } |
127 | |||
128 | /**
|
||
129 | * returns a list (blanck-separated) in one unique string
|
||
130 | *
|
||
131 | *
|
||
132 | * @return
|
||
133 | * @param input
|
||
134 | */
|
||
135 | public static String getBlankSeparated(String[] input) { |
||
136 | 3143 | jorpiell | return getXSeparated(" " , input); |
137 | 3566 | jorpiell | } |
138 | |||
139 | /**
|
||
140 | * returns a list (X-Character-separated) in one unique string
|
||
141 | *
|
||
142 | *
|
||
143 | * @return
|
||
144 | * @param X
|
||
145 | * @param input
|
||
146 | */
|
||
147 | private static String getXSeparated(String X, String[] input) { |
||
148 | 2724 | jorpiell | String output = ""; |
149 | if (input.length == 0) { |
||
150 | return ""; |
||
151 | } |
||
152 | output = input[0];
|
||
153 | for (int i = 1; i < input.length; i++) |
||
154 | 3143 | jorpiell | output = output + X + input[i]; |
155 | 3566 | jorpiell | return output;
|
156 | } |
||
157 | 2724 | jorpiell | |
158 | 3566 | jorpiell | /**
|
159 | * This method creates an String of words (blanck separated)
|
||
160 | * enveloped by asteriscs: Example
|
||
161 | * "This is an example" returns
|
||
162 | * "*This* *is* *an* *example*"
|
||
163 | *
|
||
164 | *
|
||
165 | * @return String
|
||
166 | * @param array Input String
|
||
167 | */
|
||
168 | public static String addAsteriscsFromAnArray(String array) { |
||
169 | 3456 | jorpiell | String[] s = array.split(" "); |
170 | String output = ""; |
||
171 | for (int i=0 ; i<s.length ; i++){ |
||
172 | output = output + " *" + s[i] + "*"; |
||
173 | } |
||
174 | return output;
|
||
175 | 3566 | jorpiell | } |
176 | |||
177 | /**
|
||
178 | * This function is used to try if a string is a number.
|
||
179 | *
|
||
180 | *
|
||
181 | * @return The numbers of the String
|
||
182 | * @param str Input String
|
||
183 | * @param charNumber Number of chars that must be tried
|
||
184 | */
|
||
185 | public static String tryIfIsNumber(String str, int charNumber) { |
||
186 | 3460 | jorpiell | char[] source = str.toCharArray(); |
187 | if (source.length < charNumber)
|
||
188 | charNumber = source.length; |
||
189 | char[] resultado = new char[charNumber]; |
||
190 | int j = 0; |
||
191 | for (int i = 0; i < resultado.length; i++) |
||
192 | if (i < (source.length) && (source[i] >= '0') && (source[i] <= '9')) |
||
193 | resultado[j++] = source[i]; |
||
194 | return new String(resultado,0,j); |
||
195 | 3566 | jorpiell | } |
196 | 3751 | jorpiell | |
197 | 3993 | jorpiell | /**
|
198 | * This method remove all the string accents
|
||
199 | * @param str
|
||
200 | * Input String
|
||
201 | * @return
|
||
202 | * String withOut accents
|
||
203 | */
|
||
204 | public static String removeAccents(String str){ |
||
205 | str = str.replace ('?','a'); |
||
206 | str = str.replace ('?','e'); |
||
207 | str = str.replace ('?','i'); |
||
208 | str = str.replace ('?','o'); |
||
209 | str = str.replace ('?','u'); |
||
210 | str = str.replace ('?','A'); |
||
211 | str = str.replace ('?','E'); |
||
212 | str = str.replace ('?','I'); |
||
213 | str = str.replace ('?','O'); |
||
214 | str = str.replace ('?','a'); |
||
215 | str = str.replace ('?','e'); |
||
216 | str = str.replace ('?','i'); |
||
217 | str = str.replace ('?','o'); |
||
218 | str = str.replace ('?','u'); |
||
219 | str = str.replace ('?','A'); |
||
220 | str = str.replace ('?','E'); |
||
221 | str = str.replace ('?','I'); |
||
222 | str = str.replace ('?','O'); |
||
223 | str = str.replace ('?','U'); |
||
224 | str = str.replace ('?','a'); |
||
225 | str = str.replace ('?','e'); |
||
226 | str = str.replace ('?','i'); |
||
227 | str = str.replace ('?','o'); |
||
228 | str = str.replace ('?','u'); |
||
229 | str = str.replace ('?','A'); |
||
230 | str = str.replace ('?','E'); |
||
231 | str = str.replace ('?','I'); |
||
232 | str = str.replace ('?','O'); |
||
233 | str = str.replace ('?','U'); |
||
234 | return str;
|
||
235 | } |
||
236 | 3751 | jorpiell | |
237 | 3993 | jorpiell | /**
|
238 | * This method returns all the forms than can have a
|
||
239 | * word to do an advanced search. A form could be
|
||
240 | * to change all the characters to lower case, other one
|
||
241 | * could be to change only the first character to
|
||
242 | * upper case, ...
|
||
243 | * @param str
|
||
244 | * Input string
|
||
245 | * @return
|
||
246 | * A vector of strings
|
||
247 | */
|
||
248 | public static Vector allWordForms(String str,boolean withAccents){ |
||
249 | Vector v = new Vector(); |
||
250 | |||
251 | if (!withAccents){
|
||
252 | str = str = Strings.removeAccents(str); |
||
253 | } |
||
254 | v.add(str); |
||
255 | v.add(str.toLowerCase()); |
||
256 | v.add(str.toUpperCase()); |
||
257 | v.add(Strings.getUperCaseFirst(str)); |
||
258 | if (withAccents){
|
||
259 | str = Strings.removeAccents(str); |
||
260 | v.add(str.toLowerCase()); |
||
261 | v.add(str.toUpperCase()); |
||
262 | v.add(Strings.getUperCaseFirst(str)); |
||
263 | } |
||
264 | return v;
|
||
265 | } |
||
266 | |||
267 | /**
|
||
268 | * It returns an string with the firs chacarter in UperCase
|
||
269 | * @param str
|
||
270 | * Input str
|
||
271 | * @return
|
||
272 | */
|
||
273 | public static String getUperCaseFirst(String str){ |
||
274 | if (str.length() == 0){ |
||
275 | return str;
|
||
276 | } |
||
277 | |||
278 | if (str.length() == 1){ |
||
279 | return str.toUpperCase();
|
||
280 | } |
||
281 | |||
282 | return str.substring(0,1).toUpperCase() + str.substring(1,str.length()).toLowerCase(); |
||
283 | } |
||
284 | |||
285 | |||
286 | 3566 | jorpiell | } |