View Javadoc
1   /*
2    * Copyright 2007 Werner Guttmann
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.castor.core.util;
15  
16  import org.apache.commons.lang3.StringUtils;
17  
18  /**
19   * Common functionality relate to String processing.
20   *
21   * @since 1.2
22   */
23  public class StringUtil {
24  
25    /**
26     * Replaces all occurences of a pattern within a String.
27     * 
28     * @param source The source string.
29     * @param toReplace The character to replace.
30     * @param replacement The replacement.
31     * @return The new String with characters replaced.
32     */
33    public static String replaceAll(final String source, final String toReplace,
34        final String replacement) {
35      // if (source == null) {
36      // return null;
37      // }
38      // String returnValue = source;
39      // int idx = source.lastIndexOf(toReplace);
40      // if (idx != -1) {
41      // StringBuffer ret = new StringBuffer(source);
42      // ret.replace(idx, idx + toReplace.length(), replacement);
43      // while ((idx = source.lastIndexOf(toReplace, idx - 1)) != -1) {
44      // ret.replace(idx, idx + toReplace.length(), replacement);
45      // }
46      // returnValue = ret.toString();
47      // }
48      // return returnValue;
49      return StringUtils.replace(source, toReplace, replacement);
50    }
51  
52  }