StringUtils里的isEmpty方法和isBlank方法的区别详解

 更新时间:2020年04月04日 12:00:26   作者:延迟满足  
这篇文章主要介绍了StringUtils里的isEmpty方法和isBlank方法的区别详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

前言

我们常说的字符串为空,其实就是一个没有字符的空数组。比如:

String a = "";

a 就可以称为是一个空字符串。由于 String 在 Java 中底层是通过 char 数组去存储字符串的,所以空字符串对应的 char 数组表现形式为 

private final char value[] = new char[0];

但实际工作中,我们需要对字符串进行一些校验,比如:是否为 null,是否为空,是否去掉空格、换行符、制表符等也不为空。我们一般都是通过一些框架的工具类去做这些判断,比如:apache 的 commons jar 包。下面就讲述一下常见的两个字符串校验方法以及它们的区别。

isEmpty()

public static boolean isEmpty(String str) {    
  return str == null || str.length() == 0;
}

isBlank()

public static boolean isBlank(String str) {
    int strLen;
    if (str != null && (strLen = str.length()) != 0) {
      for(int i = 0; i < strLen; ++i) {
        // 判断字符是否为空格、制表符、tab
        if (!Character.isWhitespace(str.charAt(i))) {  
          return false;
        }
      }
      return true;
    } else {
      return true;
    }
  }

结论

通过以上代码对比我们可以看出:

1.isEmpty 没有忽略空格参数,是以是否为空和是否存在为判断依据。

2.isBlank 是在 isEmpty 的基础上进行了为空(字符串都为空格、制表符、tab 的情况)的判断。(一般更为常用)

大家可以看下面的例子去体会一下。

StringUtils.isEmpty("yyy") = false
StringUtils.isEmpty("") = true
StringUtils.isEmpty("  ") = false
 
StringUtils.isBlank("yyy") = false
StringUtils.isBlank("") = true

实例展示

自定义判断方法,实现同样的判断逻辑

  /**
   * 判断对象是否为null,不允许空白串
   *
   * @param object  目标对象类型
   * @return
   */
  public static boolean isNull(Object object){
    if (null == object) {
      return true;
    }
    if ((object instanceof String)){
      return "".equals(((String)object).trim());
    }
    return false;
  }

  /**
   * 判断对象是否不为null
   *
   * @param object
   * @return
   */
  public static boolean isNotNull(Object object){
    return !isNull(object);
  }
System.out.println(StringHandler.isNull(null));    //true
System.out.println(StringHandler.isNull(""));     //true
System.out.println(StringHandler.isNull("  "));    //true
System.out.println(StringHandler.isNull("dd"));    //false

通常我们通过HttpServletRequest获取到的参数,需要经过判空处理,转型然后得到我们想要的值,这里可以进行这些操作的简单封装.如下

    /**
   * 从<code>HttpServletRequest</code>中获取<code>String</code>类型的值, 不允许传递空串
   * 
   * @param request
   *               @see HttpServletRequest
   * @param paramName
   *               参数名称
   * @return
   *               返回需要的值
   */
  public static final String getString(HttpServletRequest request,String paramName){
    return getString(request, paramName, false);
  }
  

  /**
   * 从<code>HttpServletRequest</code>中获取<code>String</code>类型的值
   * 
   * 如果传递过来的参数为包含空白字符串的字符,认为为有效值, 否则返回null
   * 
   * @param request
   *               @see HttpServletRequest
   * @param paramName
   *               参数名称
   * @return
   *               返回需要的值
   */
  public static final String getString(HttpServletRequest request,String paramName,boolean isWithSpace) {
    String tmp = request.getParameter(paramName);
    if(isWithSpace){
      //如果允许包含空格,则使用isEmpty判空
      if (!StringUtils.isEmpty(tmp)){
        return tmp;
      }
    }else{
      if(!StringUtils.isBlank(tmp)){
        return tmp;
      }
    }
    return null;
  }
  
  
  
  /**
   * 从<code>HttpServletRequest</code>中获取<code>Long</code>类型的值
   *
   * @param request
   *               @see HttpServletRequest
   * @param paramName
   *               参数名称
   * @return
   *               返回需要的值
   */
  public static final Long getLong(HttpServletRequest request,String paramName) {
    return getLong(request, paramName, -1L);
  }


  /**
   * 从<code>HttpServletRequest</code>中获取<code>Long</code>类型的值
   *
   * @param request
   *               @see HttpServletRequest
   * @param paramName
   *               参数名称
   * @param defaultValue
   *               默认值
   * @return
   *               返回需要的值
   */
  public static final Long getLong(HttpServletRequest request,String paramName,Long defaultValue) {
    String tmp = request.getParameter(paramName);
    if (!StringUtils.isBlank(tmp)){
      try {
        Long value = Long.parseLong(tmp);
        return value;
      } catch (NumberFormatException e) {
        return -1L;
      }
    }
    return defaultValue;
  }
  

  /**
   * 从<code>HttpServletRequest</code>中获取<code>Integer</code>类型的值
   *
   * @param request
   *               @see HttpServletRequest
   * @param paramName
   *               参数名称
   * @return
   *               返回需要的值
   */
  
  public static final Integer getInt(HttpServletRequest request,String paramName) {
    return getInt(request,paramName, -1);
  }

  /**
   * 从<code>HttpServletRequest</code>中获取<code>Integer</code>类型的值
   *
   * @param request
   *               @see HttpServletRequest
   * @param paramName
   *               参数名称
   * @param defaultValue
   *               默认值
   * @return
   *               返回需要的值
   */
  public static final Integer getInt(HttpServletRequest request,String paramName, int defaultValue) {
    String tmp = request.getParameter(paramName);
    if (!StringUtils.isBlank(tmp)){
      try {
        Integer value = Integer.parseInt(tmp);
        return value;
      } catch (NumberFormatException e) {
        return -1;
      }
    }
    return defaultValue;
  }
  
  /**
   * 从<code>HttpServletRequest</code>中获取<code>Short</code>类型的值
   *
   * @param request
   *               @see HttpServletRequest
   * @param paramName
   *               参数名称
   * @return
   *               返回需要的值
   */
  
  public static final Short getShort(HttpServletRequest request,String paramName) {
    return getShort(request,paramName, (short)-1);
  }

  /**
   * 从<code>HttpServletRequest</code>中获取<code>Short</code>类型的值
   *
   * @param request
   *               @see HttpServletRequest
   * @param paramName
   *               参数名称
   * @param defaultValue
   *               默认值
   * @return
   *               返回需要的值
   */
  public static final Short getShort(HttpServletRequest request,String paramName, short defaultValue) {
    String tmp = request.getParameter(paramName);
    if (!StringUtils.isBlank(tmp)){
      try {
        Short value = Short.parseShort(tmp);
        return value;
      } catch (NumberFormatException e) {
        return (short)-1;
      }
    }
    return defaultValue;
  }
  
  
  /**
   * 从<code>HttpServletRequest</code>中获取<code>Byte</code>类型的值
   *
   * @param request
   *               @see HttpServletRequest
   * @param paramName
   *               参数名称
   * @return
   *               返回需要的值
   */
  
  public static final Byte getByte(HttpServletRequest request,String paramName) {
    return getByte(request,paramName, (byte)-1);
  }

  /**
   * 从<code>HttpServletRequest</code>中获取<code>Byte</code>类型的值
   *
   * @param request
   *               @see HttpServletRequest
   * @param paramName
   *               参数名称
   * @param defaultValue
   *               默认值
   * @return
   *               返回需要的值
   */
  public static final Byte getByte(HttpServletRequest request,String paramName, Byte defaultValue) {
    String tmp = request.getParameter(paramName);
    if (!StringUtils.isBlank(tmp)){
      try {
        Byte value = Byte.parseByte(tmp);
        return value;
      } catch (NumberFormatException e) {
        return (byte)-1;
      }
    }
    return defaultValue;
  }
  
  

  /**
   * 从<code>HttpServletRequest</code>中获取<code>Double</code>类型的值
   *
   * @param request
   *               @see HttpServletRequest
   * @param paramName
   *               参数名称
   * @return
   *               返回需要的值
   */
  public static final Double getDouble(HttpServletRequest request,String paramName) {
    return getDouble(request, paramName,-1D);
  }
  

  /**
   * 从<code>HttpServletRequest</code>中获取<code>Double</code>类型的值
   *
   * @param request
   *               @see HttpServletRequest
   * @param paramName
   *               参数名称
   * @param defaultValue
   *               默认值
   * @return
   *               返回需要的值
   */
  public static final Double getDouble(HttpServletRequest request,String paramName, Double defaultValue) {
    String tmp = request.getParameter(paramName);
    if (!StringUtils.isBlank(tmp)){
      try {
        Double value = Double.parseDouble(tmp);
        return value;
      } catch (NumberFormatException e) {
        return -1D;
      }
    }
    return defaultValue;
  }
  
  

  /**
   * 从<code>HttpServletRequest</code>中获取<code>Float</code>类型的值
   *
   *        
   * @param request
   *               @see HttpServletRequest
   * @param paramName
   *               参数名称
   * @return
   *               返回需要的值
   */
  public static final Float getFloat(HttpServletRequest request,String paramName) {
    return getFloat(request, paramName,-1F);
  }
  

  /**
   * 从<code>HttpServletRequest</code>中获取<code>Float</code>类型的值
   *
   * @param request
   *               @see HttpServletRequest
   * @param paramName
   *               参数名称
   * @param defaultValue
   *               默认值
   * @return
   *               返回需要的值
   */
  public static final Float getFloat(HttpServletRequest request,String paramName, Float defaultValue) {
    String tmp = request.getParameter(paramName);
    if (!StringUtils.isBlank(tmp)){
      try {
        Float value = Float.parseFloat(tmp);
        return value;
      } catch (NumberFormatException e) {
        return -1F;
      }
    }
    return defaultValue;
  }

到此这篇关于StringUtils里的isEmpty方法和isBlank方法的区别详解的文章就介绍到这了,更多相关StringUtils isEmpty isBlank 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

最新评论