Java Array Search replaceAll(String text, char[] toSearch, char toReplace)

Here you can find the source of replaceAll(String text, char[] toSearch, char toReplace)

Description

Replaces all occurrences of a search sign with the replacement sign.

License

Open Source License

Parameter

Parameter Description
text The text to search and replace in.
toSearch The signs to search.
toReplace The sign to replace with.

Return

The new created .

Declaration

public static String replaceAll(String text, char[] toSearch, char toReplace) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 Karlsruhe Institute of Technology, Germany
 *                    Technical University Darmstadt, Germany
 *                    Chalmers University of Technology, Sweden
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  w  ww  .  j a  va2s.  c  o m*/
 *    Technical University Darmstadt - initial API and implementation and/or initial documentation
 *******************************************************************************/

import java.util.Arrays;

public class Main {
    /**
     * Replaces all occurrences of a search sign with the replacement sign.
     * @param text The text to search and replace in.
     * @param toSearch The signs to search.
     * @param toReplace The sign to replace with.
     * @return The new created {@link String}.
     */
    public static String replaceAll(String text, char[] toSearch, char toReplace) {
        if (text != null && toSearch != null) {
            // Sort toSearch
            Arrays.sort(toSearch);
            // Create new String.
            char[] signs = text.toCharArray();
            for (int i = 0; i < signs.length; i++) {
                int index = Arrays.binarySearch(toSearch, signs[i]);
                if (index >= 0) {
                    signs[i] = toReplace;
                }
            }
            return new String(signs);
        } else {
            return text;
        }
    }
}

Related

  1. isInArray(final T ch, final T[] a)
  2. isInArray(String needle, String[] haystack)
  3. memchr(boolean[] ary, int start, int len, boolean find)
  4. memchr(char[] haystack, int offset, char needle, int num)
  5. memchr(T[] ptr, T value)
  6. search(double[] input, int[] bounds, double x)
  7. Search(int[] in, int val)
  8. searchFirst(String[] target, String key)
  9. searchInStringArray(String key, String[] data)