KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > util > StringUtil


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
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 implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.util;
19
20 public abstract class StringUtil {
21     public static boolean endsWithIgnoreCase(String JavaDoc name, String JavaDoc suffix) {
22         return name.toLowerCase().endsWith(suffix.toLowerCase());
23     }
24
25     public static String JavaDoc afterFirst(String JavaDoc separator, String JavaDoc str) {
26         int pos = str.indexOf(separator);
27         if (pos != -1) {
28             return str.substring(pos + separator.length());
29         } else {
30             return str;
31         }
32     }
33
34     public static String JavaDoc afterLast(String JavaDoc separator, String JavaDoc str) {
35         int pos = str.lastIndexOf(separator);
36         if (pos != -1) {
37             return str.substring(pos + separator.length());
38         } else {
39             return str;
40         }
41     }
42
43     public static String JavaDoc beforeFirst(String JavaDoc separator, String JavaDoc str) {
44         int pos = str.indexOf(separator);
45         if (pos != -1) {
46             return str.substring(0, pos);
47         } else {
48             return str;
49         }
50     }
51
52     public static String JavaDoc beforeLast(String JavaDoc separator, String JavaDoc str) {
53         int pos = str.lastIndexOf(separator);
54         if (pos != -1) {
55             return str.substring(0, pos);
56         } else {
57             return str;
58         }
59     }
60
61     public static String JavaDoc afterLastSlashOrDot(String JavaDoc str) {
62         int pos = str.lastIndexOf('/');
63         int dot = str.lastIndexOf('.');
64         if (pos != -1 && dot > pos) {
65             pos = dot;
66         }
67         if (pos == -1) {
68             return "";
69         } else {
70             return str.substring(pos + 1);
71         }
72     }
73
74     public static boolean equalOrBothNull(String JavaDoc a, String JavaDoc b) {
75         if (a == null) {
76             return b == null;
77         } else {
78             return b != null && a.equals(b);
79         }
80     }
81
82     /**
83      * * Mangle an arbitrary string to produce a valid Java identifier.
84      */

85     public static String JavaDoc getJavaIdentifier(String JavaDoc str) {
86         // TODO: revise mangling
87
int n = str.length();
88         StringBuffer JavaDoc s = new StringBuffer JavaDoc(n);
89         for (int i = 0; i < n; i++) {
90             char c = str.charAt(i);
91             if (c == '_') {
92                 s.append("__");
93             } else if (i == 0) {
94                 if (!Character.isJavaIdentifierStart(c)) {
95                     s.append("_");
96                 } else {
97                     s.append(c);
98                 }
99             } else {
100                 if (!Character.isJavaIdentifierPart(c)) {
101                     s.append('_');
102                     s.append((int) c);
103                     s.append('_');
104                 } else {
105                     s.append(c);
106                 }
107             }
108         }
109         return s.toString();
110     }
111
112     public static String JavaDoc getLowerFirst(String JavaDoc name) {
113         if (name.length() == 0) {
114             return name;
115         } else {
116             return Character.toLowerCase(name.charAt(0)) + name.substring(1);
117         }
118     }
119
120     public static String JavaDoc getLowerFirstIfFirst2NotUpper(String JavaDoc name) {
121         if (name.length() >= 2) {
122             char c1 = name.charAt(0);
123             char c2 = name.charAt(1);
124             if (Character.isUpperCase(c1) && Character.isUpperCase(c2)) {
125                 return name;
126             }
127         }
128         return getLowerFirst(name);
129     }
130
131     public static String JavaDoc getUpperFirst(String JavaDoc name) {
132         if (name.length() == 0) {
133             return name;
134         } else {
135             return Character.toUpperCase(name.charAt(0)) + name.substring(1);
136         }
137     }
138
139     public static String JavaDoc padLeft(String JavaDoc s, char c, int n) {
140         if (s.length() < n) {
141             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(n);
142             padLeftAppend(sb, s, c, n);
143             return sb.toString();
144         } else {
145             return s;
146         }
147     }
148
149     public static void padLeftAppend(StringBuffer JavaDoc sb, String JavaDoc s, char c, int n) {
150         int p = n - s.length();
151         for (int i = 0; i < p; i++) {
152             sb.append(c);
153         }
154         sb.append(s);
155     }
156
157     public static String JavaDoc padRight(String JavaDoc s, char c, int n) {
158         if (s.length() < n) {
159             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(n);
160             padRightAppend(sb, s, c, n);
161             return sb.toString();
162         } else {
163             return s;
164         }
165     }
166
167     public static void padRightAppend(StringBuffer JavaDoc sb, String JavaDoc s, char c, int n) {
168         sb.append(s);
169         int p = n - s.length();
170         for (int i = 0; i < p; i++) {
171             sb.append(c);
172         }
173     }
174
175     public static boolean startsWithIgnoreCase(String JavaDoc name, String JavaDoc prefix) {
176         return name.toLowerCase().startsWith(prefix.toLowerCase());
177     }
178
179     public static String JavaDoc removePrefix(String JavaDoc name, String JavaDoc prefix) {
180         if (name.startsWith(prefix)) {
181             return name.substring(prefix.length());
182         } else {
183             return name;
184         }
185     }
186
187     public static String JavaDoc removePrefixIgnoreCase(String JavaDoc name, String JavaDoc prefix) {
188         if (startsWithIgnoreCase(name, prefix)) {
189             return name.substring(prefix.length());
190         } else {
191             return name;
192         }
193     }
194
195     public static String JavaDoc removeSuffix(String JavaDoc name, String JavaDoc suffix) {
196         if (name.endsWith(suffix)) {
197             return name.substring(0, name.length() - suffix.length());
198         } else {
199             return name;
200         }
201     }
202
203     public static String JavaDoc removeSuffixIgnoreCase(String JavaDoc name, String JavaDoc suffix) {
204         if (endsWithIgnoreCase(name, suffix)) {
205             return name.substring(0, name.length() - suffix.length());
206         } else {
207             return name;
208         }
209     }
210
211     /**
212      * * Replace all occurrences of a substring with another substring.
213      */

214     public static String JavaDoc replace(String JavaDoc str, String JavaDoc what, String JavaDoc with) {
215         int pos = str.indexOf(what);
216         if (pos == -1) {
217             return str;
218         }
219         return str.substring(0, pos) + with + replace(str.substring(pos + what.length()), what, with);
220     }
221
222     /**
223      * * Replace first occurrence of a substring with another substring.
224      */

225     public static String JavaDoc replaceFirst(String JavaDoc str, String JavaDoc what, String JavaDoc with) {
226         int pos = str.indexOf(what);
227         if (pos == -1) {
228             return str;
229         }
230         return str.substring(0, pos) + with + str.substring(pos + what.length());
231     }
232
233     public static String JavaDoc reverse(String JavaDoc str) {
234         int n = str.length();
235         char[] chars = new char[n];
236         for (int i = 0; i < n; i++) {
237             chars[i] = str.charAt(n - i - 1);
238         }
239         return new String JavaDoc(chars);
240     }
241
242     public static String JavaDoc trimIfNotNull(String JavaDoc str) {
243         return str == null ? null : str.trim();
244     }
245 }
246
Popular Tags