KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > util > StringUtil


1 /*
2  * Cobertura - http://cobertura.sourceforge.net/
3  *
4  * Copyright (C) 2005 Jeremy Thomerson
5  *
6  * Note: This file is dual licensed under the GPL and the Apache
7  * Source License (so that it can be used from both the main
8  * Cobertura classes and the ant tasks).
9  *
10  * Cobertura is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published
12  * by the Free Software Foundation; either version 2 of the License,
13  * or (at your option) any later version.
14  *
15  * Cobertura is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Cobertura; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  */

25
26 package net.sourceforge.cobertura.util;
27
28 /**
29  * Abstract, not to be instantiated utility class for String functions.
30  *
31  * @author Jeremy Thomerson
32  */

33 public abstract class StringUtil
34 {
35
36     /**
37      * <p>
38      * Replaces all instances of "replace" with "with" from the "original"
39      * string.
40      * </p>
41      *
42      * <p>
43      * NOTE: it is known that a similar function is included in jdk 1.4 as replaceAll(),
44      * but is written here so as to allow backward compatibility to users using SDK's
45      * prior to 1.4
46      * </p>
47      *
48      * @param original The original string to do replacement on.
49      * @param replace The string to replace.
50      * @param with The string to replace "replace" with.
51      * @return The replaced string.
52      */

53     public static String JavaDoc replaceAll(String JavaDoc original, String JavaDoc replace, String JavaDoc with)
54     {
55         if (original == null)
56         {
57             return original;
58         }
59
60         final int len = replace.length();
61         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(original.length());
62         int start = 0;
63         int found = -1;
64
65         while ((found = original.indexOf(replace, start)) != -1)
66         {
67             sb.append(original.substring(start, found));
68             sb.append(with);
69             start = found + len;
70         }
71
72         sb.append(original.substring(start));
73         return sb.toString();
74     }
75
76 }
77
Popular Tags