首页 > 技术文章 > 使用TextUtils.isEmpty简单化代码

peterpan- 2016-10-08 17:50 原文

如果让你判断一个文本框是否为空(null)或者没有任何值(length=0),你会怎么怎样去写代码,很多初学者可能会这样写:

if(text==null || text.length==0)

{........}

其实没有必要这么麻烦,在android的官方API中有更方便的解决方法:Returns true if the string is null or 0-length,可以使用这个方法-TextUtils.isEmpty()

可以这样写:

if(!TextUtils.isEmpty(text))

{.....}

推荐阅读