Replaces substrings
/* * Copyright 2000,2005 wingS development team. * * This file is part of wingS (http://wingsframework.org). * * wingS is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 * of the License, or (at your option) any later version. * * Please see COPYING for the complete licence. */ import java.util.StringTokenizer; /** * Some string manipulation utilities. * * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a> */ public class StringUtil { /** * replaces substrings with content 'toFind' with 'replace' in * s and returns the result ('s/$toFind/$replace/g') * * @param s The String the substrings should be replaced in. * @param toFind The substring to be replaced * @param replace The replacement. * @return the string with all replacements. */ public static final String replace(String s, String toFind, String replace) { StringBuilder erg = new StringBuilder(); int lastindex = 0; int indexOf = s.indexOf(toFind); if (indexOf == -1) return s; while (indexOf != -1) { erg.append(s.substring(lastindex, indexOf)).append(replace); lastindex = indexOf + toFind.length(); indexOf = s.indexOf(toFind, lastindex); } erg.append(s.substring(lastindex)); return erg.toString(); } }