Java Array Replace replaceAll(final Object[] objs, final String str)

Here you can find the source of replaceAll(final Object[] objs, final String str)

Description

Returns stripped value from the specified array of stuff

License

Open Source License

Parameter

Parameter Description
objs a parameter
str Values you want to remove

Declaration

public static Object[] replaceAll(final Object[] objs, final String str) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Main {
    /**// www. j  ava2s  .  co  m
     * Returns stripped value from the specified array of stuff
     * 
     * @param objs
     * @param str
     *            Values you want to remove
     * @return
     */
    public static Object[] replaceAll(final Object[] objs, final String str) {
        if (isEmpty(objs, false)) {
            return null;
        }
        List<Object> list = new ArrayList<Object>(Arrays.asList(trimArray(objs)));
        list.removeAll(Collections.singletonList(str));
        return list.toArray(new Object[list.size()]);
    }

    /**
     * <p>
     * Checks if an array of Object Array is empty or {@code null}.
     * </p>
     * 
     * @param objs
     * @param isContentsCheck
     * @return {@code true} if the Object Array is empty or {@code null}
     */
    public static boolean isEmpty(final Object[] objs, Boolean isContentsCheck) {
        if (objs == null || objs.length < 1) {
            return true;
        }
        if (isContentsCheck) {
            for (Object obj : objs) {
                if (isEmpty(obj)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * <p>
     * Checks if an array of Object is empty or {@code null}.<br>
     * Each Object element is a String class will call if {@link #isEmptyStr (String)}
     * </p>
     * 
     * @param obj
     * @return {@code true} if the Object is blank or {@code null}
     */
    public static boolean isEmpty(final Object obj) {
        if (obj == null) {
            return true;
        }
        if (obj instanceof String) {
            return isEmptyStr((String) obj);
        }
        return false;
    }

    /**
     * @deprecated
     * @param objs
     * @return
     */
    @Deprecated
    private static Object[] trimArray(Object[] objs) {
        if (isEmpty(objs)) {
            return null;
        }
        int i = 0;
        Object[] cpObjs = new Object[objs.length];
        for (Object obj : objs) {
            try {
                cpObjs[i] = obj.toString().trim();
            } catch (Exception e) {
                cpObjs[i] = obj;
            } finally {
                i++;
            }
        }
        return cpObjs;
    }

    /**
     * <p>
     * Checks if an array of String is empty or {@code null}.
     * </p>
     * 
     * @deprecated
     * @param str
     * @return {@code true} if the String is empty or {@code null}
     */
    @Deprecated
    private static boolean isEmptyStr(final String str) {
        return str == null || str.length() < 1;
    }
}

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. replace(char[][] arrays, char character, char[][] replacements)
  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)
  8. replaceIgnoreCase(final String s, final String[] sub, final String[] with)