KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > util > Strings


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307, USA
18  */

19
20 package edu.umd.cs.findbugs.util;
21
22 /** A class for static String utility methods.
23  * @author Brian Cole
24  */

25 public class Strings {
26
27     /** This is intended to be semantically equivalent to <code>source.replace(find, repl)</code>
28      * but also compatible with JDK 1.4.
29      * @param source The String on which to operate
30      * @param find The literal substring to be replaced
31      * @param repl The literal replacement substring
32      * @return The resultant String after substitution
33      * @throws NullPointerException if any of the arguments are null
34      * @throws IllegalArgumentException if <code>find</code> has zero length
35      * @see java.lang.String#replace(CharSequence target, CharSequence replacement)
36      */

37     public static String JavaDoc replace(String JavaDoc source, String JavaDoc find, String JavaDoc repl) {
38         /* JDK 1.5 uses a regex (with Pattern.LITERAL) to implement this. We could do
39            that too, but why don't we just use StringBuffer (not 1.5's StringBuilder) */

40         int j = source.indexOf(find); // -1 if not found
41
if (j < 0) return source; // nothing to replace
42
final int findLen = find.length();
43         if (findLen <= 0) throw new IllegalArgumentException JavaDoc("unable to replace all occurrences of the empty String");
44         int anchor = 0;
45         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(source.length()+repl.length()); // guess final length
46
while (j >= 0) {
47             sb.append(source.substring(anchor, j));
48             sb.append(repl);
49             anchor = j+findLen;
50             j = source.indexOf(find, anchor);
51         }
52         sb.append(source.substring(anchor));
53         return sb.toString();
54     }
55
56     /** This is intended to be equivalent to <code>Arrays.toString(a)</code>
57      * but also compatible with JDK 1.4.
58      * This concatenates the results of calling String.valueOf() on each element
59      * of the array, so this won't work well for multi-dimensional arrays.
60      * @see java.lang.String#valueOf(Object)
61      * @see java.util.Arrays#toString(Object[])
62      * @see java.util.Arrays#deepToString(Object[])
63      */

64     public static String JavaDoc toString(final Object JavaDoc[] a) {
65         if (a == null) return "null";
66         int max = a.length - 1;
67         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("[");
68         for (int j=0; j <= max; j+=1) {
69             sb.append(String.valueOf(a[j]));
70             if (j < max) sb.append(','); // Arrays.toString() appends ", "
71
}
72         sb.append(']');
73         return sb.toString();
74     }
75     
76     /**
77      * Trim trailing comma from given string.
78      *
79      * @param s a string
80      * @return the same string with trailing comma trimmed (if any)
81      */

82     public static String JavaDoc trimComma(String JavaDoc s) {
83         if (s.endsWith(",")) {
84             s = s.substring(0, s.length() - 1);
85         }
86         return s;
87     }
88
89 }
90
Popular Tags