Java Stream Operation replaceStr(StringBuilder original, char replacementChar, Stream stringsToReplace)

Here you can find the source of replaceStr(StringBuilder original, char replacementChar, Stream stringsToReplace)

Description

Replaces the occurrence of each string in stringsToReplace with the replacementChar in the original string.

License

Apache License

Parameter

Parameter Description
original the string to perform a replacement on
replacementChar the character used to replace each character of a matching string
stringsToReplace strings that may appear in the original string which are subject to replacement

Return

the original StringBuilder instance, which may have had a replacement operation performed on it

Declaration

static StringBuilder replaceStr(StringBuilder original, char replacementChar, Stream<String> stringsToReplace) 

Method Source Code

    //package com.java2s;
    /*//w ww.  j a v a  2 s.  c  om
     * Copyright 2017 Johns Hopkins University
     *
     * 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.
     */

    import java.util.stream.Stream;

    public class Main {
        /**
         * Replaces the occurrence of each string in {@code stringsToReplace} with the {@code replacementChar}
         * in the {@code original} string.
         * <p>
         * Examples:
         * </p>
         * <p>
         * original: "foo"
         * replacementChar: 'X'
         * stringsToReplace: { "baz" }
         * result: "foo"
         * The string "baz" never appears in the original string.
         * </p>
         * <p>
         * original: "foo"
         * replacementChar: 'X'
         * stringsToReplace: { "o" }
         * result: "fXX"
         * The string "o" occurs twice in the original string.
         * </p>
         * <p>
         * original: "foo"
         * replacementChar: 'X'
         * stringsToReplace: { "fo" }
         * result: "XXo"
         * The string "fo" occurs once in the original string.
         * </p>
         * <p>
         * original: "foo"
         * replacementChar: 'X'
         * stringsToReplace: { "of" }
         * result: "foo"
         * The string "of" never appears in the original string
         * </p>
         *
         * @param original the string to perform a replacement on
         * @param replacementChar the character used to replace each character of a matching string
         * @param stringsToReplace strings that may appear in the original string which are subject to replacement
         * @return the {@code original} StringBuilder instance, which may have had a replacement operation performed on it
         */
        static StringBuilder replaceStr(StringBuilder original, char replacementChar, Stream<String> stringsToReplace) {
    stringsToReplace.forEach(toReplace ->
    {
        int index = -1;
        while ((index = original.indexOf(toReplace)) > -1) {
            for (int replacementIndex = index; replacementIndex < (index + toReplace.length()); replacementIndex++) {
                original.setCharAt(replacementIndex, replacementChar);
            }
        }
    });

    return original;
}
    }

Related

  1. prepend(final T first, final Stream rest)
  2. rangeStream(int n, boolean parallel)
  3. readStream(Stream s, boolean lowercase)
  4. rebuildParallel(Stream stream)
  5. replacePos(StringBuilder original, char replacementChar, Stream positionsToReplace)
  6. reverse(Stream stream)
  7. reverse(Stream input)
  8. startsWith(Stream stream, Iterable iterable)
  9. startsWith(Stream stream, Stream stream2)