可以编写一个Java方法来判断多个字符串参数,只要有一个不为空,就返回结果为true。可以使用可变参数来接收多个字符串参数,并使用一个for循环来遍历这些参数,判断每个参数是否为空。如果有一个参数不为空,则返回true,否则返回false。

以下是一个示例代码:

public class StringUtils {
    public static boolean checkAnyStringNotEmpty(String... strings) {
        for (String str : strings) {
            if (str != null && !str.isEmpty()) {
                return true;
            }
        }
        return false;
    }
}

使用示例:

public class Main {
    public static void main(String[] args) {
        System.out.println(StringUtils.checkAnyStringNotEmpty("abc", "", "def")); // true
        System.out.println(StringUtils.checkAnyStringNotEmpty("", "", "")); // false
        System.out.println(StringUtils.checkAnyStringNotEmpty("")); // false
        System.out.println(StringUtils.checkAnyStringNotEmpty(null, "xyz")); // true
    }
}

输出结果:

true
false
false
true

原文地址: https://www.cveoy.top/t/topic/pK2F 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录