svn-gvsig-desktop / branches / gvSIG-mobile / libCompatMobile / src / org / gvsig / compat / StringUtils.java @ 20386
History | View | Annotate | Download (4.42 KB)
1 |
package org.gvsig.compat; |
---|---|
2 |
|
3 |
import java.util.regex.Pattern; |
4 |
|
5 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
6 |
*
|
7 |
* Copyright (C) 2006 Prodevelop and Generalitat Valenciana.
|
8 |
*
|
9 |
* This program is free software; you can redistribute it and/or
|
10 |
* modify it under the terms of the GNU General Public License
|
11 |
* as published by the Free Software Foundation; either version 2
|
12 |
* of the License, or (at your option) any later version.
|
13 |
*
|
14 |
* This program is distributed in the hope that it will be useful,
|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17 |
* GNU General Public License for more details.
|
18 |
*
|
19 |
* You should have received a copy of the GNU General Public License
|
20 |
* along with this program; if not, write to the Free Software
|
21 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
22 |
*
|
23 |
* For more information, contact:
|
24 |
*
|
25 |
* Generalitat Valenciana
|
26 |
* Conselleria d'Infraestructures i Transport
|
27 |
* Av. Blasco Ib??ez, 50
|
28 |
* 46010 VALENCIA
|
29 |
* SPAIN
|
30 |
*
|
31 |
* +34 963862235
|
32 |
* gvsig@gva.es
|
33 |
* http://www.gvsig.gva.es
|
34 |
*
|
35 |
* or
|
36 |
*
|
37 |
* Prodevelop Integraci?n de Tecnolog?as SL
|
38 |
* Conde Salvatierra de ?lava , 34-10
|
39 |
* 46004 Valencia
|
40 |
* Spain
|
41 |
*
|
42 |
* +34 963 510 612
|
43 |
* +34 963 510 968
|
44 |
* gis@prodevelop.es
|
45 |
* http://www.prodevelop.es
|
46 |
*
|
47 |
* or
|
48 |
*
|
49 |
* Instituto de Rob?tica
|
50 |
* Apartado de correos 2085
|
51 |
* 46071 Valencia
|
52 |
* (Spain)
|
53 |
*
|
54 |
* +34 963 543 577
|
55 |
* jjordan@robotica.uv.es
|
56 |
* http://robotica.uv.es
|
57 |
*
|
58 |
*/
|
59 |
/* CVS MESSAGES:
|
60 |
* -------------
|
61 |
* $Id$
|
62 |
* $Log$
|
63 |
*
|
64 |
*/
|
65 |
/**
|
66 |
* StringUtils is a class that includes some missing "java.lang.String" methods needed by gvSIG Mobile
|
67 |
* because are very used by many gvSIG classes.
|
68 |
* hasn't in "java.lang.String"
|
69 |
*
|
70 |
* @PRODEVELOP S.L.
|
71 |
* @author Carlos S?nchez Peri??n (csanchez@prodevelop.es)
|
72 |
* @version %I%, %G%
|
73 |
*/
|
74 |
public class StringUtils { |
75 |
/**
|
76 |
* Splits an String by seeking for a pattern into it, this pattern could be another sub-string
|
77 |
* when this sub-string is finded out into the input string, the string is splited in two, the first one is formed from
|
78 |
* the beggining to the pattern and goes to the first place into the String array, the second one is formed form the end
|
79 |
* of the pattern to the end of the string and goes to the second free position into the string array and is seeked again
|
80 |
* to find more patterns. This process is repited until the end of the string.
|
81 |
* <p>
|
82 |
* The return is one array with sub-strings that none of them contains the pattern string.
|
83 |
*
|
84 |
* @param input The input string to be splited by a pattern.
|
85 |
* @param pattern The pattern to split the string, could be one char or another string.
|
86 |
*
|
87 |
* @return String[] wich contains the input splited "n" times.
|
88 |
*
|
89 |
* @see java.lang.String.split(String sep) original method.
|
90 |
* @since 1.0
|
91 |
*/
|
92 |
public static String[] splitString(String input, String sep) { |
93 |
return Pattern.compile(sep).split(input, 0); |
94 |
} |
95 |
|
96 |
/*
|
97 |
* splitChar(String,char) : static method similar to split() but only seek for a char to split beyond it
|
98 |
*/
|
99 |
public static String[] splitChar(String origin, char expresion){ |
100 |
String ns[]; |
101 |
String aux = origin;
|
102 |
int indexFin;
|
103 |
int indexStart=0; |
104 |
int i=0; |
105 |
|
106 |
//take the string in couples of words separates by :
|
107 |
indexFin = origin.indexOf(expresion); |
108 |
if(indexFin!=-1){ |
109 |
while (indexFin!=-1){ |
110 |
aux = aux.substring(indexFin+1);
|
111 |
indexFin = aux.indexOf(expresion); |
112 |
i++; |
113 |
} |
114 |
ns = new String[i+1]; |
115 |
aux = origin; |
116 |
i=0;
|
117 |
indexFin = aux.indexOf(expresion); |
118 |
while (indexFin!=-1){ |
119 |
ns[i]= aux.substring(0,indexFin);
|
120 |
indexStart=indexFin+1;
|
121 |
aux = aux.substring(indexStart); |
122 |
indexFin = aux.indexOf(expresion); |
123 |
i++; |
124 |
} |
125 |
ns[i]=aux; |
126 |
} |
127 |
else
|
128 |
{ |
129 |
ns = new String[1]; |
130 |
ns[0] = origin;
|
131 |
} |
132 |
return ns;
|
133 |
} |
134 |
|
135 |
/*
|
136 |
* replaceAllString(String,String,String) : static method similar to replaceAllt() by java.lang.String.replaceAll(String,String)
|
137 |
*/
|
138 |
public static String replaceAllString(String original, String match_old, |
139 |
String match_new) {
|
140 |
return Pattern.compile(match_old).matcher(original).replaceAll( |
141 |
match_new); |
142 |
} |
143 |
} |