【问题标题】:How to check a string is not null?如何检查字符串不为空?
【发布时间】:2011-07-26 20:50:34
【问题描述】:
if(string.equals(""))
{

}

如何判断字符串是否为空?

if(!string.equals(""))
{

}

【问题讨论】:

标签: java


【解决方案1】:

您可以使用以下代码:

 if (string != null) {

 }

【讨论】:

    【解决方案2】:

    检查 null 是通过 if (string != null) 完成的

    如果你想检查它是 null 还是空 - 你需要if (string != null && !string.isEmpty())

    我更喜欢用commons-langStringUtils.isNotEmpty(..)

    【讨论】:

    • StringUtils 再次+1。我们每个人都应该每天花几分钟时间阅读 apache commons 可以为我们做什么!
    【解决方案3】:
    if(string != null)
    

    if(string.length() == 0)
    

    if(("").equals(string))
    

    【讨论】:

    • 并非如此。只有第一个是真的。
    【解决方案4】:

    你可以试试这个

    if(string != null)
    

    【讨论】:

    • 多个空格怎么样
    【解决方案5】:

    检查 null 由以下人员完成:

    string != null
    

    您的示例实际上是在检查空字符串

    您可以像这样将两者结合起来:

    if (string != null && !string.equals("")) { ...
    

    但是 null 和 empty 是两个不同的东西

    【讨论】:

      【解决方案6】:

      正如大家所说,您必须在您正在测试内存指针的对象中检查 (string!=null)。

      因为每个对象都由内存指针标识,所以在测试其他任何内容之前,您必须检查对象是否为空指针,因此:

      (string!=null && !string.equals("")) 很好

      (!string.equals("") && string !=null) 可以给你一个空指针异常。

      如果你不关心尾随空格,你总是可以在 equals() 之前使用 trim() 所以 " " 和 "" 给你同样的结果

      【讨论】:

        【解决方案7】:

        没有什么新东西可以添加到上面的答案中,只是将它包装到一个简单的类中。 Commons-lang 完全没问题,但如果您只需要这些或更多辅助函数,滚动您自己的简单类是最简单的方法,同时还可以减小可执行文件的大小。

        public class StringUtils {
        
          public static boolean isEmpty(String s) {
            return (s == null || s.isEmpty());
          }
        
          public static boolean isNotEmpty(String s) {
            return !isEmpty(s);
          }
        }
        

        【讨论】:

          【解决方案8】:
          if(str != null && !str.isEmpty())
          

          请务必按此顺序使用 && 的部分,因为如果 && 的第一部分失败,java 将不会继续评估第二部分,从而确保您不会从 str.isEmpty() 获得空指针异常,如果str 为空。

          请注意,它仅从 Java SE 1.6 开始可用。

          你必须检查str.length() == 0 or str.equals("") 在以前的版本上。

          【讨论】:

            【解决方案9】:

            使用 TextUtils 方法。

            TextUtils.isEmpty(str) :如果字符串为空或长度为 0,则返回 true。参数:str 要检查的字符串返回:如果 str 为 null 或长度为零,则为 true

            if(TextUtils.isEmpty(str)){
                // str is null or lenght is 0
            }
            

            TextUtils 类的Source

            isEmpty 方法:

             public static boolean isEmpty(CharSequence str) {
                    if (str == null || str.length() == 0)
                        return true;
                    else
                        return false;
                }
            

            【讨论】:

            【解决方案10】:

            检查字符串的最佳方法是:

            import org.apache.commons.lang3.StringUtils;
            
            if(StringUtils.isNotBlank(string)){
            ....
            }
            

            来自doc

            isBlank(CharSequence cs) :

            检查 CharSequence 是否为空 (""),null 或仅空格。

            【讨论】:

            • 您应该指定它的库全名Commons,并更具体地说明方法行为。恕我直言,您应该稍微改进一下答案。问候
            • @FabianoTarlao 确定的朋友,已经更新了答案!
            【解决方案11】:

            您可以使用 Predicate 及其新方法(自 java 11 起)Predicate::not

            您可以编写代码来检查字符串是否不为空且不为空:

            Predicate<String> notNull = Predicate.not(Objects::isNull);
            Predicate<String> notEmptyString = Predicate.not(String::isEmpty);
            Predicate<String> isNotEmpty = notNull.and(notEmptyString);
            

            那你就可以测试了:

            System.out.println(isNotEmpty.test(""));      // false
            System.out.println(isNotEmpty.test(null));    // false
            System.out.println(isNotEmpty.test("null"));  // true
            

            【讨论】:

              【解决方案12】:

              在 Java 中测试空字符串的常用方法是使用 Optionals:

              Optional.ofNullable(myString).orElse("value was null")
              
              Optional.ofNullable(myString).ifPresent(s -> System.out.println(s));
              
              Optional.ofNullable(myString).orElseThrow(() -> new RuntimeException("value was null"));
              

              要测试它是 null 还是空,您可以使用 Apache org.apache.commons.lang3 库,它为您提供以下方法:

              • StringUtils.isEmpty(String) / StringUtils.isNotEmpty(String): 测试String是null还是空(“”不为空)
              • StringUtils.isBlank(String) / StringUtils.isNotBlank(String): 与 isEmpty bt 相同,如果字符串只是空格,则视为空白

              并申请Optional你得到:

              Optional.ofNullable(myString).filter(StringUtils::isNotEmpty).orElse("value was null or empty");
              

              【讨论】:

              • 不要这样做。它只是复杂,对接受的答案没有任何好处。
              • 它是一个与java if(myString != null){ ... } 相同的单行器,您觉得它很复杂,但我假设您发现 Java lambda 也很难阅读?
              猜你喜欢
              • 1970-01-01
              • 2011-11-25
              • 2012-03-23
              • 2011-04-05
              • 2015-10-29
              • 2013-03-07
              相关资源
              最近更新 更多