01: /*******************************************************************************
02: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
03: * Thomschke.
04: *
05: * All Rights Reserved. This program and the accompanying materials
06: * are made available under the terms of the Eclipse Public License v1.0
07: * which accompanies this distribution, and is available at
08: * http://www.eclipse.org/legal/epl-v10.html
09: *
10: * Contributors:
11: * Sebastian Thomschke - initial implementation.
12: *******************************************************************************/package net.sf.oval.internal.util;
13:
14: import java.util.Collection;
15:
16: /**
17: * @author Sebastian Thomschke
18: */
19: public final class StringUtils {
20: public static String implode(final Collection<?> values,
21: final String delimiter) {
22: return implode(values.toArray(), delimiter);
23: }
24:
25: public static String implode(final Object[] values,
26: final String delimiter) {
27: if (values == null)
28: return "";
29:
30: final StringBuilder out = new StringBuilder();
31:
32: for (int i = 0, l = values.length; i < l; i++) {
33: if (i > 0)
34: out.append(delimiter);
35: out.append(values[i]);
36: }
37: return out.toString();
38: }
39:
40: /**
41: * high-performance case-sensitive string replacement
42: */
43: public static String replaceAll(final String searchIn,
44: final String searchFor, final String replaceWith) {
45: final StringBuilder out = new StringBuilder();
46:
47: int searchFrom = 0, foundAt = 0;
48: final int searchForLength = searchFor.length();
49:
50: while ((foundAt = searchIn.indexOf(searchFor, searchFrom)) >= 0) {
51: out.append(searchIn.substring(searchFrom, foundAt)).append(
52: replaceWith);
53: searchFrom = foundAt + searchForLength;
54: }
55:
56: return out.append(
57: searchIn.substring(searchFrom, searchIn.length()))
58: .toString();
59: }
60:
61: /**
62: * private constructor
63: */
64: private StringUtils() {
65: // do nothing
66: }
67: }
|