Kotlin中的空判断

前言

Kotlin中一切皆对象,这里我们理解成对象的空判断。由于Kotlin是空安全的,普通可空对象可以通过 ?. 来避免产生空指针异常。

...
val p: Person? = null
println("age is : ${
      
      p?.age}")
...

就算p对象为null,运行的时候也不会产生空指针异常而导致应用退出,打印如下:

age is : null

而这里我们主要讨论两种特殊对象的空判断:字符串和集合。

字符串空判断

在Android Studio中我们可以看到系统提供了一系列API来辅助我们进行字符串空判断:

  • isBlank()
  • isEmpty()
  • isNullOrEmpty()
  • isNullOrBlank()
  • isNotBlank()
  • isNotEmpty()

就一个简单的空判断为什么整这么多方法?而且他们长的还贼像,下面我们分别来进行打印看看这些方法具体什么用途。

不过可能每个人或者每个项目中业务逻辑对空字符的定义不尽相同,我们选取最常见的四种情况来进行试验:

1""
2" "
3null
4"null"

空字符串:""

fun main() {
    
    
    var test: String? = ""
    println("test isBlank : ${
      
      test?.isBlank()}")
    println("test isEmpty : ${
      
      test?.isEmpty()}")
    println("test isNullOrEmpty : ${
      
      test.isNullOrEmpty()}")
    println("test isNullOrBlank : ${
      
      test.isNullOrBlank()}")
    println("test isNotBlank : ${
      
      test?.isNotBlank()}")
    println("test isNotEmpty : ${
      
      test?.isNotEmpty()}")
}

打印结果:

test isBlank : true
test isEmpty : true
test isNullOrEmpty : true
test isNullOrBlank : true
test isNotBlank : false
test isNotEmpty : false

可以看到isBlank 、isEmpty 、isNullOrEmpty 、isNullOrBlank 这几个函数都返回true,而isNotBlank 和isNotEmpty 从名字看应该分别对应isBlank 和isEmpty ,返回相反的值,后面就着重看isBlank 和isEmpty 这两个函数。从这个结果来看对于空字符都能返回预期结果。

纯空格字符串:" "

fun main() {
    
    
    var test: String? = " "
    println("test isBlank : ${
      
      test?.isBlank()}")
    println("test isEmpty : ${
      
      test?.isEmpty()}")
    println("test isNullOrEmpty : ${
      
      test.isNullOrEmpty()}")
    println("test isNullOrBlank : ${
      
      test.isNullOrBlank()}")
    println("test isNotBlank : ${
      
      test?.isNotBlank()}")
    println("test isNotEmpty : ${
      
      test?.isNotEmpty()}")
}

打印结果:

test isBlank : true
test isEmpty : false
test isNullOrEmpty : false
test isNullOrBlank : true
test isNotBlank : false
test isNotEmpty : true

可以看到这个打印结果和前面比有了些许区别。对于项目中把纯空格字符串定义为空的,isBlank 和isNullOrBlank 能返回预期结果,而isEmpty 和isNullOrEmpty 则不能。

null字符串:null

fun main() {
    
    
    var test: String? = null
    println("test isBlank : ${
      
      test?.isBlank()}")
    println("test isEmpty : ${
      
      test?.isEmpty()}")
    println("test isNullOrEmpty : ${
      
      test.isNullOrEmpty()}")
    println("test isNullOrBlank : ${
      
      test.isNullOrBlank()}")
    println("test isNotBlank : ${
      
      test?.isNotBlank()}")
    println("test isNotEmpty : ${
      
      test?.isNotEmpty()}")
}

打印结果:

test isBlank : null
test isEmpty : null
test isNullOrEmpty : true
test isNullOrBlank : true
test isNotBlank : null
test isNotEmpty : null

从结果中可以看到isNullOrEmpty 和isNullOrBlank 对于值为null的字符串能返回预期结果,其他则因为空安全都返回null值。如果你不想返回null值可以配合 ?: 使用来返回你期望的结果。

fun main() {
    
    
    var test: String? = null
    println("test isBlank : ${
      
      test?.isBlank() ?: true}")
    println("test isEmpty : ${test?.isEmpty() ?: ""}")
    println("test isNullOrEmpty : ${
      
      test.isNullOrEmpty()}")
    println("test isNullOrBlank : ${
      
      test.isNullOrBlank()}")
    println("test isNotBlank : ${test?.isNotBlank() ?: "1"}")
    println("test isNotEmpty : ${test?.isNotEmpty() ?: "null"}")
}

打印结果:

test isBlank : true
test isEmpty : 
test isNullOrEmpty : true
test isNullOrBlank : true
test isNotBlank : 1
test isNotEmpty : null

值为null字符串:“null”

fun main() {
    
    
    var test: String? = "null"
    println("test isBlank : ${
      
      test?.isBlank()}")
    println("test isEmpty : ${
      
      test?.isEmpty()}")
    println("test isNullOrEmpty : ${
      
      test.isNullOrEmpty()}")
    println("test isNullOrBlank : ${
      
      test.isNullOrBlank()}")
    println("test isNotBlank : ${
      
      test?.isNotBlank()}")
    println("test isNotEmpty : ${
      
      test?.isNotEmpty()}")
}

打印结果:

test isBlank : false
test isEmpty : false
test isNullOrEmpty : false
test isNullOrBlank : false
test isNotBlank : true
test isNotEmpty : true

一般来说很少会认为值为null的字符串会被当做空,从打印结果我们也印证了这一点。如果你期望把值为null的字符在当做空,则调用这些函数不能返回你预期的结果。

下面我们就通过一张表格来统一展现这几个函数对于通常四种被认为是空的字符串的执行结果。

值/函数 isBlank isEmpty isNullOrBlank isNullOrEmpty isNotBlank isNotEmpty
="" true true true true false false
=" " true false true false false true
=null null null true true null null
=“null” false false false false true true

大家可以参考上表根据项目中对空的定义来酌情使用。

集合空判断

这里就选择我们常用的List集合来进行验证。在Android Studio中我们可以看到系统提供了几个和字符串判空类似的API来辅助我们进行集合空判断:

  • isEmpty()
  • isNotEmpty()
  • isNullOrEmpty()

对于集合我们通常认为空的一般就两种情况:

1、集合值为null
2、集合size为0

集合值为null

fun main() {
    
    
    var testList: MutableList<String>? = null
    println("testList isEmpty : ${
      
      testList?.isEmpty()}")
    println("testList isNotEmpty : ${
      
      testList?.isNotEmpty()}")
    println("testList isNullOrEmpty : ${
      
      testList.isNullOrEmpty()}")
}

打印结果:

testList isEmpty : null
testList isNotEmpty : null
testList isNullOrEmpty : true

可以看到只有isNullOrEmpty 返回了我们预期的结果,而另外两个函数由于空安全返回null值。这里同样可以配合 ?" 使用来返回你预期的值。

集合size为0

fun main() {
    
    
    var testList: MutableList<String>? = mutableListOf()
    println("testList isEmpty : ${
      
      testList?.isEmpty()}")
    println("testList isNotEmpty : ${
      
      testList?.isNotEmpty()}")
    println("testList isNullOrEmpty : ${
      
      testList.isNullOrEmpty()}")
}

打印结果:

testList isEmpty : true
testList isNotEmpty : false
testList isNullOrEmpty : true

当集合size为0的时候通过isNullOrEmpty 和isEmpty 都能返回预期的结果。

同样我们把结果通过表格来展示。由于其他集合类再使用此类判空API时都会有和List相同的结果,所以这里只展示List的结果。

值/函数 isEmpty isNotEmpty isNullOrEmpty
=null null null true
size为0 true false true

尾巴

今天的学习和总结就这么多了,如果文章中有什么错误,欢迎指正。喜欢我的文章,欢迎一键三连:点赞,评论,关注,谢谢大家!

猜你喜欢

转载自blog.csdn.net/abs625/article/details/110187235