Java String Replace replace(String source, String stringToReplace, String replacementString, boolean matchCase)

Here you can find the source of replace(String source, String stringToReplace, String replacementString, boolean matchCase)

Description

Searches for a string within a specified string and replaces every match with another string.

License

Apache License

Parameter

Parameter Description
source The string in which the matching parts will be replaced.
stringToReplace The string that will be searched for.
replacementString The string that will replace each matching part.
matchCase A <code>boolean</code> indicating if the match is going to be performed in a case-sensitive manner or not.

Return

A new String object containing the replacement result.

Declaration

public static String replace(String source, String stringToReplace, String replacementString,
        boolean matchCase) 

Method Source Code

//package com.java2s;
/*//from   w w w. ja va  2 s .  c  om
 * Copyright 2001-2013 Geert Bevin (gbevin[remove] at uwyn dot com)
 * Licensed under the Apache License, Version 2.0 (the "License")
 */

import java.util.*;

public class Main {
    /**
     * Searches for a string within a specified string in a case-sensitive
     * manner and replaces every match with another string.
     *
     * @param source            The string in which the matching parts will be replaced.
     * @param stringToReplace   The string that will be searched for.
     * @param replacementString The string that will replace each matching
     *                          part.
     * @return A new <code>String</code> object containing the replacement
     *         result.
     * @since 1.0
     */
    public static String replace(String source, String stringToReplace, String replacementString) {
        return replace(source, stringToReplace, replacementString, true);
    }

    /**
     * Searches for a string within a specified string and replaces every
     * match with another string.
     *
     * @param source            The string in which the matching parts will be replaced.
     * @param stringToReplace   The string that will be searched for.
     * @param replacementString The string that will replace each matching
     *                          part.
     * @param matchCase         A <code>boolean</code> indicating if the match is
     *                          going to be performed in a case-sensitive manner or not.
     * @return A new <code>String</code> object containing the replacement
     *         result.
     * @since 1.0
     */
    public static String replace(String source, String stringToReplace, String replacementString,
            boolean matchCase) {
        if (null == source) {
            return null;
        }

        if (null == stringToReplace) {
            return source;
        }

        if (null == replacementString) {
            return source;
        }

        Iterator<String> string_parts = split(source, stringToReplace, matchCase).iterator();
        StringBuilder new_string = new StringBuilder();

        while (string_parts.hasNext()) {
            String string_part = string_parts.next();
            new_string.append(string_part);
            if (string_parts.hasNext()) {
                new_string.append(replacementString);
            }
        }

        return new_string.toString();
    }

    /**
     * Splits a string into different parts, using a seperator string to
     * detect the seperation boundaries in a case-sensitive manner. The
     * seperator will not be included in the list of parts.
     *
     * @param source    The string that will be split into parts.
     * @param seperator The seperator string that will be used to determine
     *                  the parts.
     * @return An <code>ArrayList</code> containing the parts as
     *         <code>String</code> objects.
     * @since 1.0
     */
    public static List<String> split(String source, String seperator) {
        return split(source, seperator, true);
    }

    /**
     * Splits a string into different parts, using a seperator string to
     * detect the seperation boundaries. The seperator will not be included in
     * the list of parts.
     *
     * @param source    The string that will be split into parts.
     * @param seperator The seperator string that will be used to determine
     *                  the parts.
     * @param matchCase A <code>boolean</code> indicating if the match is
     *                  going to be performed in a case-sensitive manner or not.
     * @return An <code>ArrayList</code> containing the parts as
     *         <code>String</code> objects.
     * @since 1.0
     */
    public static List<String> split(String source, String seperator, boolean matchCase) {
        ArrayList<String> substrings = new ArrayList<>();

        if (null == source) {
            return substrings;
        }

        if (null == seperator) {
            substrings.add(source);
            return substrings;
        }

        int current_index = 0;
        int delimiter_index;
        String element;

        String source_lookup_reference;
        if (!matchCase) {
            source_lookup_reference = source.toLowerCase();
            seperator = seperator.toLowerCase();
        } else {
            source_lookup_reference = source;
        }

        while (current_index <= source_lookup_reference.length()) {
            delimiter_index = source_lookup_reference.indexOf(seperator, current_index);

            if (-1 == delimiter_index) {
                element = new String(source.substring(current_index, source.length()));
                substrings.add(element);
                current_index = source.length() + 1;
            } else {
                element = new String(source.substring(current_index, delimiter_index));
                substrings.add(element);
                current_index = delimiter_index + seperator.length();
            }
        }

        return substrings;
    }
}

Related

  1. getLineBreakOffsets(String replacementString)
  2. replace(final String text, final String search, final String replace)
  3. replace(String origStr, char oldChar, String newStr)
  4. replace(String source, Properties properties)
  5. replace(String source, String oldChars, String newChars)
  6. replace(String str)
  7. replace(String string, String pattern, String value)
  8. replace(String string, String pattern, String value)
  9. replace(String target, String replacement, String... args)