Java Array Remove removeFlag(String[] options, String flag)

Here you can find the source of removeFlag(String[] options, String flag)

Description

Checks whether the flag is in the option string and removes the flag if present.

License

Open Source License

Parameter

Parameter Description
options the options to check
flag the flag to look for (incl. "-")

Return

true if a help flag is among the options

Declaration

public static boolean removeFlag(String[] options, String flag) 

Method Source Code

//package com.java2s;
/*// w  ww . ja  v  a  2s  .c  om
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.List;

public class Main {
    /**
     * Checks whether the flag is in the option string and removes the flag
     * if present.
     *
     * @param options   the options to check
     * @param flag   the flag to look for (incl. "-")
     * @return      true if a help flag is among the options
     */
    public static boolean removeFlag(String[] options, String flag) {
        boolean result;
        int i;

        result = false;

        for (i = 0; i < options.length; i++) {
            if (options[i].equals(flag)) {
                options[i] = "";
                result = true;
                break;
            }
        }

        return result;
    }

    /**
     * Checks whether the flag is in the option string and removes the flag
     * if present.
     *
     * @param options   the options to check
     * @param flag   the flag to look for (incl. "-")
     * @return      true if a help flag is among the options
     */
    public static boolean removeFlag(List<String> options, String flag) {
        boolean result;
        int i;

        result = false;

        for (i = 0; i < options.size(); i++) {
            if (options.get(i).equals(flag)) {
                options.set(i, "");
                result = true;
                break;
            }
        }

        return result;
    }
}

Related

  1. removeFirst(String[] array)
  2. removeFirst(String[] in)
  3. removeFirst(String[] strArr)
  4. removeFirstElementFromArray(T[] array)
  5. removeFirstTwoArgs(String[] args, int startIndex)
  6. removeFlagsFromArgs(String[] args)
  7. removeFrom(double[] source, int idx)
  8. removeFromArray(String remove, String array[])
  9. removeFromArray(String[] oldArray, String stringToRemove)