KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > base > util > TString


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.base.util;
12
13 /**
14  * This class provides static methods for some of the very used String
15  * operations
16  */

17 public class TString {
18     // change all occurrences of oldPat to newPat
19
public static String JavaDoc change(String JavaDoc in, String JavaDoc oldPat, String JavaDoc newPat) {
20         if (oldPat.length() == 0)
21             return in;
22         if (oldPat.length() == 1 && newPat.length() == 1)
23             return in.replace(oldPat.charAt(0), newPat.charAt(0));
24         if (in.indexOf(oldPat) < 0)
25             return in;
26         int lastIndex = 0;
27         int newIndex = 0;
28         StringBuffer JavaDoc newString = new StringBuffer JavaDoc();
29         for (;;) {
30             newIndex = in.indexOf(oldPat, lastIndex);
31             if (newIndex != -1) {
32                 newString.append(in.substring(lastIndex, newIndex) + newPat);
33                 lastIndex = newIndex + oldPat.length();
34             } else {
35                 newString.append(in.substring(lastIndex));
36                 break;
37             }
38         }
39         return newString.toString();
40     }
41 }
42
Popular Tags