Kotlin中的空判断

  • 前言
  • 字符串空判断
    • 空字符串:""
    • 纯空格字符串:" "
    • null字符串:null
    • 值为null字符串:"null"
  • 集合空判断
    • 集合值为null
    • 集合size为0
  • 尾巴

前言

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、" "
3、null
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? = nullprintln("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? = nullprintln("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>? = nullprintln("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

尾巴

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

Kotlin中的空判断相关推荐

  1. 教你如何完全解析Kotlin中的类型系统

    简述: 已经很久没有更新文章,这大概是2019年第二篇文章了,有很多小伙伴们都在公众号留言说是不是断更了.是不是跑路了.在这里统一回复下我还好,并没有跑路哈,只是在思考接下来文章主要方向在哪? 如何在 ...

  2. JavaScript空判断

    在JavaScript中,空判断比较常见,但究竟应该如何正确地使用空判断呢?不同的数据类型有不同的判断方法,不能同一而论,在判断空前,应先确定数据的类型. 1.不同类型具有不同的判空方法 在判空前应预 ...

  3. Kotlin中的一些判空操作、空安全

    前言 很久没有更新博客了,其实并不是因为太忙,也不是遇到的问题不多,是迷茫了,不知道怎么去写,总感觉自己一定会去好好的写文章(哈哈幻想中),但是从来没有动过笔,因为写不好,能力不够.但是还是要去动笔呀 ...

  4. JAVA中equals空_Java中为什么查询空字符串跟判断是否为null的时候可以不用equals?...(亲测)

    if(id!=null&&id!="") 代码中经常这样写,空字符串跟null 都不用equals吗? 首先,你的 id!="" 这种写法是错误 ...

  5. Mybatis if test 中int integer判断非空的坑

    Mybatis 中,alarmType 是int类型.如果alarmType 为0的话,条件判断返回结果为false,其它值的话,返回true. 1 <if test="alarmTy ...

  6. hive 判断子字符串_Java中检查空字符串(null或空白)的方法有几种?

    说明: 本文为牛旦教育原创,头条首发,转发须注明来源和原文网址. 1.摘要 在本文中,我们将介绍些方法检查Java中的空字符串(空的或空白符串).我们有些语言的原生方法以及几个库可用. 2.空与空白( ...

  7. kotlin中判断字符串_Kotlin程序查找字符串中字符的频率

    kotlin中判断字符串 Given a string and a character, we have to find the frequency of the character in the s ...

  8. kotlin中判断字符串_Kotlin程序删除字符串中所有出现的字符

    kotlin中判断字符串 Given a string and a character, we have to remove all occurrences of the character in g ...

  9. MySQL数据库中where条件查询(比较运算符查询、逻辑运算符查询、模糊查询、范围查询、空判断查询)

    1. where条件查询的介绍 where条件查询可以对表中的数据进行筛选,条件成立的记录会出现在结果集中. where语句支持的运算符: 比较运算符 逻辑运算符 模糊查询 范围查询 空判断 wher ...

最新文章

  1. Datawhale组队学习周报(第017周)
  2. Leetcode 剑指 Offer 40. 最小的k个数 (每日一题 20210825)
  3. 中国34城最全剖析:深圳、天津的短板与不足,何时才能补?
  4. POJ 2483 Cows(树状数组)
  5. java main方法调用非静态方法_java中main函数怎么调用外部非static方法
  6. [html] 如何判断用户正在操作页面?当页面一个小时没有操作时跳转到指定页面如何做?
  7. 达摩院最新AI技术助力天猫双11,提供接近真人的语音交互体验
  8. 代码审查工具Sonar下载、安装、使用
  9. 叙述计算机的主要应用领域并各举实例说明,大学计算机基础练习习题集.doc
  10. [高效Mac] 多显示器快速切换鼠标焦点和移动窗口
  11. android krc歌词解析,10行代码解析krc歌词文件
  12. 服务器如何从bios修改mac,BIOS维修网站www.biosrepair.com-用编程器修改网卡MAC地址
  13. 北京市朝阳区 办理 驾驶证期满换证 自助体检 的流程
  14. 上兴远控流量分析报告
  15. 基于JSoup的网络爬虫爬取小说内容
  16. latex公式斜体_latex输入斜体
  17. Cannot connect to the Maven process.Try again later.If the problem persists...
  18. C语言二级题库(卷一)
  19. 扩展无线网络(WDS桥接功能)
  20. python多分支结构案例_python的程序控制结构-分支结构与身体质量指数案例--python-6-da...

热门文章

  1. 刘韧:终生学习就是想方设法保持25岁时的智力水平
  2. Element - table固定列页面数据过多滚动时显示问题
  3. 计算机在生活中的作用80字英文作文,关于电脑的80字英语作文
  4. C语言函数调用的过程图解深入剖析
  5. FDO error:Failed to label layer(XXX) for class Default
  6. 安装Hadoop集群(超详细!)
  7. CSDN网站使用了哪些技术?
  8. UI设计自学有哪些途径?
  9. vscode配置go环境
  10. CPLEX出现‘q1‘ is not convex?