1 16 17 package org.apache.jetspeed.util; 18 19 import java.util.StringTokenizer ; 20 import java.util.Map ; 21 22 30 public class StringUtils 31 { 32 43 public static String replaceAll(String original, String find, String replacement) 44 { 45 StringBuffer buffer = new StringBuffer (original); 46 47 int idx = original.length(); 48 int offset = find.length(); 49 50 while( ( idx=original.lastIndexOf(find, idx-1) ) > -1 ) 51 { 52 buffer.replace(idx,idx+offset, replacement); 53 } 54 55 return buffer.toString(); 56 } 57 68 public static StringBuffer replaceAll(StringBuffer buffer, String find, String replacement) 69 { 70 71 int bufidx = buffer.length() - 1; 72 int offset = find.length(); 73 while( bufidx > -1 ) { 74 int findidx = offset -1; 75 while( findidx > -1 ) { 76 if( bufidx == -1 ) { 77 return buffer; 79 } 80 if( buffer.charAt( bufidx ) == find.charAt( findidx ) ) { 81 findidx--; bufidx--; 83 } else { 84 findidx = offset - 1; bufidx--; 86 if( bufidx == -1 ) { 87 return buffer; 89 } 90 continue; 91 } 92 } 93 buffer.replace( bufidx+1, 98 bufidx+1+offset, 99 replacement); 100 } 102 return buffer; 104 105 } 106 107 114 public static final String arrayToString(String [] array, String separators) 115 { 116 StringBuffer sb = new StringBuffer (""); 117 String empty = ""; 118 119 if (array == null) 120 return empty; 121 122 if (separators == null) 123 separators = ","; 124 125 for (int ix=0; ix < array.length; ix++) 126 { 127 if (array[ix] != null && !array[ix].equals("")) 128 { 129 sb.append(array[ix] + separators); 130 } 131 } 132 String str = sb.toString(); 133 if (!str.equals("")) 134 { 135 str = str.substring(0, (str.length() - separators.length())); 136 } 137 return str; 138 } 139 140 147 public static final String [] stringToArray(String str, String separators) 148 { 149 StringTokenizer tokenizer; 150 String [] array = null; 151 int count = 0; 152 153 if (str == null) 154 return array; 155 156 if (separators == null) 157 separators = ","; 158 159 tokenizer = new StringTokenizer (str, separators); 160 if ((count = tokenizer.countTokens()) <= 0) { 161 return array; 162 } 163 164 array = new String [count]; 165 166 int ix = 0; 167 while (tokenizer.hasMoreTokens()) 168 { 169 array[ix] = tokenizer.nextToken(); 170 ix++; 171 } 172 173 return array; 174 } 175 176 183 public static String removeChars (String data, String removeChars) 184 { 185 String temp = null; 186 StringBuffer out = new StringBuffer (); 187 temp = data; 188 189 StringTokenizer st = new StringTokenizer (temp, removeChars); 190 while (st.hasMoreTokens()) 191 { 192 String element = (String ) st.nextElement(); 193 out.append(element); 194 } 195 return out.toString(); 196 } 197 198 202 public static String stripExtension(String filename) 203 { 204 int index = filename.lastIndexOf('.'); 205 if (index > -1) 206 { 207 return filename.substring(0, index); 208 } 209 return filename; 210 } 211 212 221 public static String replaceVars(String origString, Map vars) 222 { 223 224 StringBuffer finalString = new StringBuffer (); 225 int index = 0; 226 int i = 0; 227 String key = null; 228 String value = null; 229 while ((index = origString.indexOf("${", i)) > -1) 230 { 231 key = origString.substring(index + 2, origString.indexOf("}", index+3)); 232 value = (String ) vars.get(key); 233 finalString.append(origString.substring(i, index)); 234 if (value != null) 235 { 236 finalString.append(value); 237 } 238 else 239 { 240 finalString.append("${"+key+"}"); 241 } 242 i = index + 3 + key.length(); 243 } 244 finalString.append (origString.substring(i)); 245 246 return finalString.toString(); 247 } 248 249 } 250 | Popular Tags |