KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > util > Strings


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Marc Wick.
20  * Contributor(s): Mathieu Peltier.
21  */

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

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

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

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