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