Java Array Replace replace(char[][] arrays, char character, char[][] replacements)

Here you can find the source of replace(char[][] arrays, char character, char[][] replacements)

Description

replace

License

Open Source License

Declaration

public static char[][] replace(char[][] arrays, char character, char[][] replacements) 

Method Source Code

//package com.java2s;
/**/*from  ww  w  .j  a va  2 s . c o  m*/
 * Aptana Studio
 * Copyright (c) 2005-2011 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the GNU Public License (GPL) v3 (with exceptions).
 * Please see the license.html included with this distribution for details.
 * Any modifications to this file must keep this entire header intact.
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static String EMPTY_STRING = "";

    public static char[][] replace(char[][] arrays, char character, char[][] replacements) {
        List<char[]> list = new ArrayList<char[]>();
        for (char[] array : arrays) {
            String string = String.valueOf(array);
            if (string.indexOf(character) >= 0) {
                for (char[] replacement : replacements) {
                    list.add(string.replaceAll(String.valueOf(character), String.valueOf(replacement))
                            .toCharArray());
                }
            } else {
                list.add(array);
            }
        }
        return list.toArray(new char[list.size()][]);
    }

    /**
     * replace in a string a string sequence with another string sequence
     */
    public static String replace(String source, String whatBefore, String whatAfter) {
        if (null == source || source.length() == 0) {
            return source;
        }
        int beforeLen = whatBefore.length();
        if (beforeLen == 0) {
            return source;
        }
        StringBuffer result = new StringBuffer(EMPTY_STRING);
        int lastIndex = 0;
        int index = source.indexOf(whatBefore, lastIndex);
        while (index >= 0) {
            result.append(source.substring(lastIndex, index));
            result.append(whatAfter);
            lastIndex = index + beforeLen;

            // get next
            index = source.indexOf(whatBefore, lastIndex);
        }
        result.append(source.substring(lastIndex));
        return result.toString();
    }
}

Related

  1. arrayReplace(String haystack, String[] search, String[] replacements)
  2. replace(char[] array, char[] toBeReplaced, char[] replacementChars)
  3. replace(char[][] arrays, char character, char[][] replacements)
  4. replaceAll(final Object[] objs, final String str)
  5. replaceAll(final String[] args, final String from, final String to)
  6. replaceAll(String src, String[] replace, String[] by)
  7. replaceChars(String s, char[] from, char[] to)