Here you can find the source of replaceAll(String text, char[] toSearch, char toReplace)
Parameter | Description |
---|---|
text | The text to search and replace in. |
toSearch | The signs to search. |
toReplace | The sign to replace with. |
public static String replaceAll(String text, char[] toSearch, char toReplace)
//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; } } }