/** * Gives a new StringArray who contains all Strings form the given Array excepting the String from the array to * remove. The first occurrence of the given Strings in the string array to remove was removed. * * @return new <code>String[]</code> without the first occurrence of the given Strings in the string array to * remove * @throws IllegalArgumentException if array is <code>null</code> */ public static String[] removeFromArray(String[] array, String[] toRemove) throws IllegalArgumentException { Guardian.assertNotNull("array", array); if (toRemove == null) { return array; } String[] newArray = array; for (String item : toRemove) { newArray = removeFromArray(newArray, item); } return newArray; }
public void testRemoveFromArray() { String[] strings; strings = StringUtils.removeFromArray(new String[]{"A", "B", "C"}, "A"); assertTrue("String[]{\"B\",\"C\"} expected", ArrayUtils.equalArrays(new String[]{"B", "C"}, strings)); strings = StringUtils.removeFromArray(new String[]{"A", "B", "C"}, "B"); assertTrue("String[]{\"A\",\"C\"} expected", ArrayUtils.equalArrays(new String[]{"A", "C"}, strings)); strings = StringUtils.removeFromArray(new String[]{"A", "B", "C"}, "C"); assertTrue("String[]{\"A\",\"B\"} expected", ArrayUtils.equalArrays(new String[]{"A", "B"}, strings)); strings = StringUtils.removeFromArray(new String[]{"A", "B", "C"}, "D"); assertTrue("String[]{\"A\",\"B\",\"C\"} expected", ArrayUtils.equalArrays(new String[]{"A", "B", "C"}, strings)); }
public void testFail() { String[] strings; strings = StringUtils.removeFromArray(new String[]{"A", "B", "C"}, new String[]{"A"}); assertTrue("String[]{\"B\",\"C\"} expected", ArrayUtils.equalArrays(new String[]{"B", "C"}, strings)); strings = StringUtils.removeFromArray(new String[]{"A", "B", "C"}, new String[]{"B"}); assertTrue("String[]{\"A\",\"C\"} expected", ArrayUtils.equalArrays(new String[]{"A", "C"}, strings)); strings = StringUtils.removeFromArray(new String[]{"A", "B", "C"}, new String[]{"C"}); assertTrue("String[]{\"A\",\"B\"} expected", ArrayUtils.equalArrays(new String[]{"A", "B"}, strings)); strings = StringUtils.removeFromArray(new String[]{"A", "B", "C"}, new String[]{"D"}); assertTrue("String[]{\"A\",\"B\",\"C\"} expected", ArrayUtils.equalArrays(new String[]{"A", "B", "C"}, strings)); strings = StringUtils.removeFromArray(new String[]{"A", "B", "C", "D"}, new String[]{"D", "B"}); assertTrue("String[]{\"A\",\"C\"} expected", ArrayUtils.equalArrays(new String[]{"A", "C"}, strings)); }