Java String Empty isAnyEmpty(String... sAry)

Here you can find the source of isAnyEmpty(String... sAry)

Description

is Any Empty

License

Open Source License

Declaration

public static boolean isAnyEmpty(String... sAry) 

Method Source Code

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import java.util.Iterator;
import java.util.List;

public class Main {
    public static boolean isAnyEmpty(String... sAry) {
        for (String s : sAry) {
            if (isEmpty(s))
                return true;
        }//w  w  w . j  a  va2s.  com
        return false;
    }

    /**
     * Returns true is the given string is either null, or an empty string.
     * A string of white spaces is also considered empty.
     * @param s a string
     * @return Returns true is the given string is either null, or an empty string.
     */
    public static boolean isEmpty(String s) {
        return s == null || trim(s).length() == 0;
    }

    /**
     * Returns true is the given string is either null, or an empty string.
     * A string of white spaces is also considered empty.
     * @param o a Object
     * @return Returns true is the given toString() is either null, or an empty string.
     */
    public static boolean isEmpty(Object o) {
        return o == null || isEmpty(o.toString());
    }

    /**
     * returns the length of the given string.
     * 0 if str is null
     * @param str
     * @return
     */
    public static int length(String str) {
        return str == null ? 0 : str.length();
    }

    /**
     * this implementation is much faster than GWt's String.trim() once
     * compiled into javascript, especially in Firefox
     * @param str
     * @return
     */
    public static String trim(String str) {
        if (str == null || str.length() == 0)
            return str;

        String whitespace = " \t\n\f\r";
        int i = 0;
        int len = str.length();
        for (; i < str.length(); i++) {
            if (whitespace.indexOf(str.charAt(i)) == -1) {
                str = str.substring(i);
                break;
            }
        }
        if (i == len) {
            // empty string...
            return "";
        }

        for (i = str.length() - 1; i >= 0; i--) {
            if (whitespace.indexOf(str.charAt(i)) == -1) {
                str = str.substring(0, i + 1);
                break;
            }
        }
        return str;
    }

    public static String toString(Object[] objs, String separatedBy) {
        if (objs != null) {
            return toString(Arrays.asList(objs), separatedBy);
        } else {
            return "";
        }
    }

    public static String toString(int[] objs, String separatedBy) {
        ArrayList<Integer> l = new ArrayList<Integer>();
        for (int i : objs) {
            l.add(i);
        }
        return toString(l, separatedBy);
    }

    /**
     * Same as {@link #toString(java.util.Collection ,String) toString}
     * separated by ", ".
     *
     * @param c
     * @return
     */
    public static String toString(Collection c) {
        return toString(c, ", ");
    }

    /**
     * Returns a String representation of this collection.
     * It will use String.valueOf(Object) to convert object to string.
     * If separatedby is given, the items will be separated by that string.
     *
     * @param c
     * @param separatedBy
     * @return
     */
    public static String toString(Collection c, String separatedBy) {
        if (c == null || c.size() == 0)
            return "";

        StringBuffer sb = new StringBuffer();
        for (Iterator itr = c.iterator(); itr.hasNext();) {
            Object o = itr.next();
            sb.append(String.valueOf(o));
            if (separatedBy != null && itr.hasNext()) {
                sb.append(separatedBy);
            }
        }

        return sb.toString();
    }

    public static String toString(Object obj) {
        return obj == null ? "null" : obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode());
    }

    /**
     * split up the given string into parts based on the given regular expression.
     * the parts are trimmed and returned as a List<String>
     * @param str
     * @param regExp
     * @return
     */
    public static List<String> asList(String str, String regExp, boolean convertToLower) {
        if (isEmpty(str))
            return null;
        String[] strs = str.split(regExp);
        ArrayList<String> list = new ArrayList<String>(strs.length);
        for (String str1 : strs) {
            if (convertToLower)
                str1 = str1.toLowerCase();
            list.add(str1.trim());
        }
        return list;
    }

    /**
     * split up the given string into parts based on the given regular expression.
     * the parts are trimmed and returned as a List<String>
     * @param str
     * @param regExp
     * @return
     */
    public static List<String> asList(String str, String regExp) {
        return asList(str, regExp, false);
    }
}

Related

  1. emptyStrings()
  2. emptyStringToLongZero(String s)
  3. emptyStringToNull(String value)
  4. firstNotEmpty(String... values)
  5. getFirstNotEmpty(String str, String sperator)
  6. isEmpty(String input)
  7. isEmpty(String param)
  8. isEmpty(String sTest_)
  9. isEmpty(String str)