KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > util > Strings


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Marc Wick.
22  * Contributor(s): Mathieu Peltier.
23  */

24
25 package org.objectweb.cjdbc.common.util;
26
27 /**
28  * This class provides utilities for Strings manipulation.
29  *
30  * @author <a HREF="mailto:mwick@dplanet.ch">Marc Wick</a>
31  * @author <a HREF="mailto:mathieu.peltier@emicnetworks.com">Mathieu Peltier</a>
32  * @version 1.0
33  */

34 public class Strings
35 {
36
37   /**
38    * Replaces all occurrences of a String within another String.
39    *
40    * @param sourceString source String
41    * @param replace text pattern to replace
42    * @param with replacement text
43    * @return the text with any replacements processed, <code>null</code> if
44    * null String input
45    */

46   public static String JavaDoc replace(String JavaDoc sourceString, String JavaDoc replace, String JavaDoc with)
47   {
48     if (sourceString == null || replace == null || with == null
49         || "".equals(replace))
50     {
51       return sourceString;
52     }
53
54     StringBuffer JavaDoc buf = new StringBuffer JavaDoc(sourceString.length());
55     int start = 0, end = 0;
56     while ((end = sourceString.indexOf(replace, start)) != -1)
57     {
58       buf.append(sourceString.substring(start, end)).append(with);
59       start = end + replace.length();
60     }
61     buf.append(sourceString.substring(start));
62     return buf.toString();
63   }
64
65   /**
66    * Replaces all occurrences of a String within another String. The String to
67    * be replaced will be replaced ignoring cases, all other cases are preserved
68    * in the returned string
69    *
70    * @param sourceString source String
71    * @param replace text to replace, case insensitive
72    * @param with replacement text
73    * @return the text with any replacements processed, <code>null</code> if
74    * null String input
75    */

76   public static String JavaDoc replaceCasePreserving(String JavaDoc sourceString,
77       String JavaDoc replace, String JavaDoc with)
78   {
79     if (sourceString == null || replace == null || with == null)
80     {
81       return sourceString;
82     }
83     String JavaDoc lower = sourceString.toLowerCase();
84     int shift = 0;
85     int idx = lower.indexOf(replace);
86     int length = replace.length();
87     StringBuffer JavaDoc resultString = new StringBuffer JavaDoc(sourceString);
88     do
89     {
90       resultString = resultString.replace(idx + shift, idx + shift + length,
91           with);
92       shift += with.length() - length;
93       idx = lower.indexOf(replace, idx + length);
94     }
95     while (idx > 0);
96
97     return resultString.toString();
98   }
99
100 }
101
Popular Tags