Java Array Contain arrayContains(final String[] array, String needle)

Here you can find the source of arrayContains(final String[] array, String needle)

Description

Returns true if the String array contains a string.

License

Open Source License

Parameter

Parameter Description
array The String-Array
needle The String to look for

Return

true if the array contains the string, false otherwise

Declaration

public static boolean arrayContains(final String[] array, String needle) 

Method Source Code

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

public class Main {
    /**/*w  w w  . j av a  2 s.c o m*/
     * Returns true if the String array contains a string.
     * 
     * @param array
     *            The String-Array
     * @param needle
     *            The String to look for
     * @return true if the array contains the string, false otherwise
     */
    public static boolean arrayContains(final String[] array, String needle) {
        needle = nn(needle);

        for (final String element : array) {
            if (needle.equalsIgnoreCase(element.trim())) {
                return true;
            }
        }

        return false;
    }

    /**
     * Converts a object to a string. If the object is null, a empty string will be returned
     * 
     * @param o
     *            The {@link Object} to convert to a string
     * @return String representation of the object
     */
    public static String nn(final Object o) {
        return nn(o, "");
    }

    /**
     * Converts a object to a string or returns the default value if the object is null
     * 
     * @param o
     *            The {@link Object} to convert to a string
     * @param def
     *            The default {@link String}
     * @return
     */
    public static String nn(final Object o, final String def) {
        return (o == null ? def : o.toString());
    }
}

Related

  1. arrayContains(char[] chars, char cToFind)
  2. arrayContains(final byte[] what, final byte[] b)
  3. arrayContains(final E[] arr, final E targetValue)
  4. arrayContains(final int[] array, final int v)
  5. arrayContains(final Object[] a, final Object v)
  6. arrayContains(final T[] array, final T v)
  7. arrayContains(int[] arr, int e)
  8. arrayContains(int[] arr, int ii)
  9. arrayContains(int[] array, int value)