Source code: com/textflex/txtfl/LibTxtfl.java
1 /* tXtFL: a text-based football simulator
2 * Copyright (C) 2002-3 Text Flex
3
4 * This file is part of tXtFL.
5
6 * tXtFL is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21
22 package com.textflex.txtfl;
23
24 /** Repository of general functions useful across tXtFL source code.
25 */
26 public class LibTxtfl {
27
28 /** Replaces all instances of one substring for another in a given
29 string.
30 @param s original string
31 @param oldSubstring substring to remove
32 @param newSubstring substring to take the place of
33 <code>oldSubstring</code>
34 @return the new string
35 */
36 public static String replaceSubstring(String s, String oldSubstring,
37 String newSubstring) {
38 int i = 0;
39 // continue while still have the old substring
40 while ((i = s.indexOf(oldSubstring)) != -1) {
41 // check for a substring past oldSubstring
42 if (s.length() > i + oldSubstring.length()) {
43 s = s.substring(0, i) + newSubstring
44 + s.substring(i + oldSubstring.length());
45 } else {
46 s = s.substring(0, i) + newSubstring;
47
48 }
49 }
50 return s;
51 }
52 }