001 /******************************************************************************* 002 * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian 003 * Thomschke. 004 * 005 * All Rights Reserved. This program and the accompanying materials 006 * are made available under the terms of the Eclipse Public License v1.0 007 * which accompanies this distribution, and is available at 008 * http://www.eclipse.org/legal/epl-v10.html 009 * 010 * Contributors: 011 * Sebastian Thomschke - initial implementation. 012 *******************************************************************************/ 013 package net.sf.oval.internal.util; 014 015 import java.util.Collection; 016 017 /** 018 * @author Sebastian Thomschke 019 */ 020 public final class StringUtils 021 { 022 public static String implode(final Collection< ? > values, final String delimiter) 023 { 024 return implode(values.toArray(), delimiter); 025 } 026 027 public static String implode(final Object[] values, final String delimiter) 028 { 029 if (values == null) return ""; 030 031 final StringBuilder out = new StringBuilder(); 032 033 for (int i = 0, l = values.length; i < l; i++) 034 { 035 if (i > 0) out.append(delimiter); 036 out.append(values[i]); 037 } 038 return out.toString(); 039 } 040 041 /** 042 * high-performance case-sensitive string replacement 043 */ 044 public static String replaceAll(final String searchIn, final String searchFor, final String replaceWith) 045 { 046 final StringBuilder out = new StringBuilder(); 047 048 int searchFrom = 0, foundAt = 0; 049 final int searchForLength = searchFor.length(); 050 051 while ((foundAt = searchIn.indexOf(searchFor, searchFrom)) >= 0) 052 { 053 out.append(searchIn.substring(searchFrom, foundAt)).append(replaceWith); 054 searchFrom = foundAt + searchForLength; 055 } 056 057 return out.append(searchIn.substring(searchFrom, searchIn.length())).toString(); 058 } 059 060 /** 061 * private constructor 062 */ 063 private StringUtils() 064 { 065 super(); 066 } 067 }