1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.cms.server.util; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import java.util.*; |
6 |
|
7 |
/** |
8 |
* A class containing useful methods for manipulating |
9 |
* String objects. |
10 |
* |
11 |
* @author $Author: tdb1 $ |
12 |
* @version $Id: StringUtils.java,v 1.3 2001/03/14 23:25:29 tdb1 Exp $ |
13 |
*/ |
14 |
public class StringUtils { |
15 |
|
16 |
//---FINAL ATTRIBUTES--- |
17 |
|
18 |
/** |
19 |
* The current CVS revision of this class |
20 |
*/ |
21 |
public static final String REVISION = "$Revision: 1.3 $"; |
22 |
|
23 |
//---STATIC METHODS--- |
24 |
|
25 |
/** |
26 |
* Searches a string and replaces all occurences |
27 |
* of the given search text with the given replacement |
28 |
* text. |
29 |
* |
30 |
* @param text the text to search and replace in |
31 |
* @param search the string to look for and replace |
32 |
* @param replace the text to replace with |
33 |
* @return the updated version of text |
34 |
*/ |
35 |
public static String replaceText(String text, String search, String replace) { |
36 |
StringBuffer textBuffer = new StringBuffer(text); |
37 |
int currIndex = 0; |
38 |
currIndex = text.indexOf(search, currIndex); |
39 |
while(currIndex != -1) { |
40 |
if (currIndex != -1) { |
41 |
textBuffer.delete(currIndex, currIndex + search.length()); |
42 |
textBuffer.insert(currIndex, replace); |
43 |
} |
44 |
text = textBuffer.toString(); |
45 |
currIndex = text.indexOf(search, currIndex + search.length()); |
46 |
} |
47 |
return new String(textBuffer); |
48 |
} |
49 |
|
50 |
/** |
51 |
* This method takes an array of String's and a |
52 |
* String to look for and returns the position |
53 |
* in the array that the string occurs. |
54 |
* |
55 |
* @param search the string to look for |
56 |
* @param array the array to look in |
57 |
* |
58 |
* @return the position in the array |
59 |
*/ |
60 |
public static int getStringPos(String search, String[] array) { |
61 |
for(int x = 0; x < array.length; x++) { |
62 |
if (array[x].equals(search)) { |
63 |
return x; |
64 |
} |
65 |
} |
66 |
return -1; |
67 |
} |
68 |
|
69 |
//---CONSTRUCTORS--- |
70 |
|
71 |
//---PUBLIC METHODS--- |
72 |
|
73 |
/** |
74 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
75 |
* method to provide clean logging (every class should have this). |
76 |
* |
77 |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
78 |
* to format the toString() |
79 |
* |
80 |
* @return the name of this class and its CVS revision |
81 |
*/ |
82 |
public String toString() { |
83 |
return FormatName.getName( |
84 |
_name, |
85 |
getClass().getName(), |
86 |
REVISION); |
87 |
} |
88 |
|
89 |
//---PRIVATE METHODS--- |
90 |
|
91 |
//---ACCESSOR/MUTATOR METHODS--- |
92 |
|
93 |
//---ATTRIBUTES--- |
94 |
|
95 |
/** |
96 |
* This is the friendly identifier of the |
97 |
* component this class is running in. |
98 |
* eg, a Filter may be called "filter1", |
99 |
* If this class does not have an owning |
100 |
* component, a name from the configuration |
101 |
* can be placed here. This name could also |
102 |
* be changed to null for utility classes. |
103 |
*/ |
104 |
private String _name = null; |
105 |
|
106 |
//---STATIC ATTRIBUTES--- |
107 |
|
108 |
} |