Java Array Replace replace(char[] array, char[] toBeReplaced, char[] replacementChars)

Here you can find the source of replace(char[] array, char[] toBeReplaced, char[] replacementChars)

Description

replace

License

Open Source License

Declaration

public static final char[] replace(char[] array, char[] toBeReplaced, char[] replacementChars) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2016 IBM Corporation and others.
 * 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:/* ww  w .  j a  v a 2  s.  co  m*/
 *     IBM Corporation - initial API and implementation
 *     Andrew Ferguson (Symbian)
 *     Markus Schorn (Wind River Systems)
 *     Sergey Prigogin (Google)
 *******************************************************************************/

import java.util.Arrays;

public class Main {
    public static final char[] replace(char[] array, char[] toBeReplaced, char[] replacementChars) {
        int max = array.length;
        int replacedLength = toBeReplaced.length;
        int replacementLength = replacementChars.length;

        int[] starts = new int[5];
        int occurrenceCount = 0;

        if (!equals(toBeReplaced, replacementChars)) {
            next: for (int i = 0; i < max; i++) {
                int j = 0;
                while (j < replacedLength) {
                    if (i + j == max)
                        continue next;
                    if (array[i + j] != toBeReplaced[j++])
                        continue next;
                }
                if (occurrenceCount == starts.length) {
                    System.arraycopy(starts, 0, starts = new int[occurrenceCount * 2], 0, occurrenceCount);
                }
                starts[occurrenceCount++] = i;
            }
        }
        if (occurrenceCount == 0)
            return array;
        char[] result = new char[max + occurrenceCount * (replacementLength - replacedLength)];
        int inStart = 0, outStart = 0;
        for (int i = 0; i < occurrenceCount; i++) {
            int offset = starts[i] - inStart;
            System.arraycopy(array, inStart, result, outStart, offset);
            inStart += offset;
            outStart += offset;
            System.arraycopy(replacementChars, 0, result, outStart, replacementLength);
            inStart += replacedLength;
            outStart += replacementLength;
        }
        System.arraycopy(array, inStart, result, outStart, max - inStart);
        return result;
    }

    public static final boolean equals(char[] str1, char[] str2) {
        return Arrays.equals(str1, str2);
    }

    public static final boolean equals(char[][] strarr1, char[][] strarr2) {
        if (strarr1 == strarr2) {
            return true;
        }
        if (strarr1 == null || strarr2 == null) {
            return false;
        }
        if (strarr1.length != strarr2.length) {
            return false;
        }
        for (int i = 0; i < strarr2.length; i++) {
            if (!Arrays.equals(strarr1[i], strarr2[i])) {
                return false;
            }
        }
        return true;
    }

    /**
     * Returns {@code true} if the contents of a character array are the same as contents
     * of a string.
     * @since 5.4
     */
    public static final boolean equals(char[] str1, String str2) {
        int length = str1.length;
        if (str2.length() != length)
            return false;

        for (int i = 0; i < length; i++) {
            if (str1[i] != str2.charAt(i))
                return false;
        }
        return true;
    }

    /**
     * Returns {@code true} if the contents of a section of a character array are the same as contents of a string.
     * 
     * @since 5.5
     */
    public static final boolean equals(char[] str1, int start1, int length1, String str2) {
        if (length1 != str2.length() || str1.length < length1 + start1)
            return false;
        for (int i = 0; i < length1; ++i) {
            if (str1[start1++] != str2.charAt(i))
                return false;
        }
        return true;
    }

    /**
     * Returns {@code true} if the contents of a section of a character array are the same as
     * contents of another character array.
     */
    public static final boolean equals(char[] str1, int start1, int length1, char[] str2) {
        if (length1 != str2.length || str1.length < length1 + start1)
            return false;
        if (str1 == str2 && start1 == 0)
            return true;
        for (int i = 0; i < length1; ++i) {
            if (str1[start1++] != str2[i])
                return false;
        }

        return true;
    }

    public static final boolean equals(char[] str1, int start1, int length1, char[] str2, boolean ignoreCase) {
        if (!ignoreCase)
            return equals(str1, start1, length1, str2);

        if (length1 != str2.length || str1.length < start1 + length1)
            return false;

        for (int i = 0; i < length1; ++i) {
            if (Character.toLowerCase(str1[start1++]) != Character.toLowerCase(str2[i]))
                return false;
        }
        return true;
    }
}

Related

  1. arrayReplace(String haystack, String[] search, String[] replacements)
  2. replace(char[][] arrays, char character, char[][] replacements)
  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)