/*
 * Copyright 2007 Werner Guttmann
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package org.castor.core.util;

import org.apache.commons.lang3.StringUtils;

/**
 * Common functionality relate to String processing.
 *
 * @since 1.2
 */
public class StringUtil {

  /**
   * Replaces all occurences of a pattern within a String.
   * 
   * @param source The source string.
   * @param toReplace The character to replace.
   * @param replacement The replacement.
   * @return The new String with characters replaced.
   */
  public static String replaceAll(final String source, final String toReplace,
      final String replacement) {
    // if (source == null) {
    // return null;
    // }
    // String returnValue = source;
    // int idx = source.lastIndexOf(toReplace);
    // if (idx != -1) {
    // StringBuffer ret = new StringBuffer(source);
    // ret.replace(idx, idx + toReplace.length(), replacement);
    // while ((idx = source.lastIndexOf(toReplace, idx - 1)) != -1) {
    // ret.replace(idx, idx + toReplace.length(), replacement);
    // }
    // returnValue = ret.toString();
    // }
    // return returnValue;
    return StringUtils.replace(source, toReplace, replacement);
  }

}
