Here you can find the source of replacePos(StringBuilder original, char replacementChar, Stream
Parameter | Description |
---|---|
original | the string to perform a replacement on |
replacementChar | the character used to replace each position |
positionsToReplace | the 0-based position(s) (index) in the original string to replace |
Parameter | Description |
---|---|
IndexOutOfBoundsException | if the position is not between 0 and original.length() |
static StringBuilder replacePos(StringBuilder original, char replacementChar, Stream<Integer> positionsToReplace)
//package com.java2s; /*/*from ww w . j a va2 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 each position in the original string with the replacement character. * * @param original the string to perform a replacement on * @param replacementChar the character used to replace each position * @param positionsToReplace the 0-based position(s) (index) in the original string to replace * @return the {@code original} StringBuilder instance, which may have had replacement operations performed on it * @throws IndexOutOfBoundsException if the position is not between 0 and original.length() */ static StringBuilder replacePos(StringBuilder original, char replacementChar, Stream<Integer> positionsToReplace) { positionsToReplace.forEach((index) -> original.setCharAt(index, replacementChar)); return original; } }