判断字符是否为空的一些方法理解

来源:互联网 发布:史丹利的寓言mac版 编辑:程序博客网 时间:2024/05/16 09:49

判断一个字符串为空的方法

String str=xxx;

if(str==null || str.equals("")){}

判断字符串是否为空,要先判断str是不是对象,再判断str是不是空字符串

以下的是错误的

f(str.equals("") ||str==null ){}

因为对象才能调用方法,null不是对象。

判断一个字符串为空,首先就要确保他不是null,然后再判断他的长度。

  String str = xxx;

  if(str != null && str.length() != 0) { }

原文查看:http://www.cnblogs.com/ayan/p/3524816.html

=====================

在org.apache.commons.lang包中提供了两种判断字符串是否为空的方法

为空的判断:

1.StringUtils.isEmpty(str)   //此方法不提供对空格的过滤

StringUtils.isEmpty("        ") = false

2.StringUtils.isBlank(str)

StringUtils.isBlank("        ") = true

不为空的判断:

1.StringUtils.isNotEmpty(str)   

2.StringUtils.isNotBlank(str)


原文查看:http://www.cnblogs.com/yaya-yaya/p/6096539.html


0 0
原创粉丝点击