KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > util > StringUtil


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.util;
20
21 public abstract class StringUtil
22 {
23     public static boolean endsWithIgnoreCase(String name, String suffix)
24     {
25         return name.toLowerCase().endsWith(suffix.toLowerCase());
26     }
27
28     public static String afterFirst(String separator, String str)
29     {
30         int pos = str.indexOf(separator);
31         if (pos != -1)
32         {
33             return str.substring(pos + separator.length());
34         }
35         else
36         {
37             return str;
38         }
39     }
40
41     public static String afterLast(String separator, String str)
42     {
43         int pos = str.lastIndexOf(separator);
44         if (pos != -1)
45         {
46             return str.substring(pos + separator.length());
47         }
48         else
49         {
50             return str;
51         }
52     }
53
54     public static String beforeFirst(String separator, String str)
55     {
56         int pos = str.indexOf(separator);
57         if (pos != -1)
58         {
59             return str.substring(0, pos);
60         }
61         else
62         {
63             return str;
64         }
65     }
66
67     public static String beforeLast(String separator, String str)
68     {
69         int pos = str.lastIndexOf(separator);
70         if (pos != -1)
71         {
72             return str.substring(0, pos);
73         }
74         else
75         {
76             return str;
77         }
78     }
79
80     public static String afterLastSlashOrDot(String str)
81     {
82         int pos = str.lastIndexOf('/');
83         int dot = str.lastIndexOf('.');
84         if (pos != -1 && dot > pos)
85         {
86             pos = dot;
87         }
88         if (pos == -1)
89         {
90             return "";
91         }
92         else
93         {
94             return str.substring(pos + 1);
95         }
96     }
97
98     public static boolean equalOrBothNull(String a, String b)
99     {
100         if (a == null)
101         {
102             return b == null;
103         }
104         else
105         {
106             return b != null && a.equals(b);
107         }
108     }
109
110     /**
111      ** Mangle an arbitrary string to produce a valid Java identifier.
112      **/

113     public static String getJavaIdentifier(String str)
114     {
115         // TODO: revise mangling
116
int n = str.length();
117         StringBuffer s = new StringBuffer(n);
118         for (int i = 0; i < n; i++)
119         {
120             char c = str.charAt(i);
121             if (c == '_')
122             {
123                 s.append("__");
124             }
125             else if (i == 0)
126             {
127                 if (! Character.isJavaIdentifierStart(c))
128                 {
129                     s.append("_");
130                 }
131                 else
132                 {
133                     s.append(c);
134                 }
135             }
136             else
137             {
138                 if (! Character.isJavaIdentifierPart(c))
139                 {
140                     s.append('_');
141                     s.append((int)c);
142                     s.append('_');
143                 }
144                 else
145                 {
146                     s.append(c);
147                 }
148             }
149         }
150         return s.toString();
151     }
152
153     public static String getLowerFirst(String name)
154     {
155         if (name.length() == 0)
156         {
157             return name;
158         }
159         else
160         {
161             return Character.toLowerCase(name.charAt(0)) + name.substring(1);
162         }
163     }
164
165     public static String getLowerFirstIfFirst2NotUpper(String name)
166     {
167         if (name.length() >= 2)
168         {
169             char c1 = name.charAt(0);
170             char c2 = name.charAt(1);
171             if (Character.isUpperCase(c1) && Character.isUpperCase(c2))
172             {
173                 return name;
174             }
175         }
176         return getLowerFirst(name);
177     }
178
179     public static String getUpperFirst(String name)
180     {
181         if (name.length() == 0)
182         {
183             return name;
184         }
185         else
186         {
187             return Character.toUpperCase(name.charAt(0)) + name.substring(1);
188         }
189     }
190
191     public static String padLeft(String s, char c, int n)
192     {
193         if (s.length() < n)
194         {
195             StringBuffer sb = new StringBuffer(n);
196             padLeftAppend(sb, s, c, n);
197             return sb.toString();
198         }
199         else
200         {
201             return s;
202         }
203     }
204
205     public static void padLeftAppend(StringBuffer sb, String s, char c, int n)
206     {
207         int p = n - s.length();
208         for (int i = 0; i < p; i++)
209         {
210             sb.append(c);
211         }
212         sb.append(s);
213     }
214
215     public static String padRight(String s, char c, int n)
216     {
217         if (s.length() < n)
218         {
219             StringBuffer sb = new StringBuffer(n);
220             padRightAppend(sb, s, c, n);
221             return sb.toString();
222         }
223         else
224         {
225             return s;
226         }
227     }
228
229     public static void padRightAppend(StringBuffer sb, String s, char c, int n)
230     {
231         sb.append(s);
232         int p = n - s.length();
233         for (int i = 0; i < p; i++)
234         {
235             sb.append(c);
236         }
237     }
238
239     public static boolean startsWithIgnoreCase(String name, String prefix)
240     {
241         return name.toLowerCase().startsWith(prefix.toLowerCase());
242     }
243
244     public static String removePrefix(String name, String prefix)
245     {
246         if (name.startsWith(prefix))
247         {
248             return name.substring(prefix.length());
249         }
250         else
251         {
252             return name;
253         }
254     }
255
256     public static String removePrefixIgnoreCase(String name, String prefix)
257     {
258         if (startsWithIgnoreCase(name, prefix))
259         {
260             return name.substring(prefix.length());
261         }
262         else
263         {
264             return name;
265         }
266     }
267
268     public static String removeSuffix(String name, String suffix)
269     {
270         if (name.endsWith(suffix))
271         {
272             return name.substring(0, name.length() - suffix.length());
273         }
274         else
275         {
276             return name;
277         }
278     }
279
280     public static String removeSuffixIgnoreCase(String name, String suffix)
281     {
282         if (endsWithIgnoreCase(name, suffix))
283         {
284             return name.substring(0, name.length() - suffix.length());
285         }
286         else
287         {
288             return name;
289         }
290     }
291
292     /**
293      ** Replace all occurrences of a substring with another substring.
294      **/

295     public static String replace(String str, String what, String with)
296     {
297         int pos = str.indexOf(what);
298         if (pos == -1)
299         {
300             return str;
301         }
302         return str.substring(0, pos) + with + replace(str.substring(pos + what.length()), what, with);
303     }
304
305     /**
306      ** Replace first occurrence of a substring with another substring.
307      **/

308     public static String replaceFirst(String str, String what, String with)
309     {
310         int pos = str.indexOf(what);
311         if (pos == -1)
312         {
313             return str;
314         }
315         return str.substring(0, pos) + with + str.substring(pos + what.length());
316     }
317
318     public static String reverse(String str)
319     {
320         int n = str.length();
321         char[] chars = new char[n];
322         for (int i = 0; i < n; i++)
323         {
324             chars[i] = str.charAt(n - i - 1);
325         }
326         return new String(chars);
327     }
328
329     public static String trimIfNotNull(String str)
330     {
331         return str == null ? null : str.trim();
332     }
333 }
334
Popular Tags