Java Type Coalesce coalesce(String... strings)

Here you can find the source of coalesce(String... strings)

Description

Same as the SQL COALESCE.

License

Open Source License

Parameter

Parameter Description
strings Any number of strings

Return

The first non empty string

Declaration

public static String coalesce(String... strings) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*w w w  .  j  ava2s.c  o m*/
     * Same as the SQL COALESCE. Will get first non empty value and return it.
     * 
     * @param strings Any number of strings
     * @return The first non empty string
     */
    public static String coalesce(String... strings) {
        for (String s : strings) {
            if (!isEmpty(s)) {
                return s;
            }
        }
        return "";
    }

    /**
     * @param s
     * @return true if s is null, empty or purely whitespace
     */
    public static boolean isEmpty(String s) {
        return s == null || s.trim().length() == 0;
    }
}

Related

  1. coalesce(Object src, Object defaultValue)
  2. coalesce(Object... args)
  3. coalesce(Object... items)
  4. coalesce(String str1, String str2)
  5. coalesce(String... strings)
  6. coalesce(String... vals)
  7. coalesce(String[] values)
  8. coalesce(T a, T b)
  9. coalesce(T o0, T o1)