Kotlin ifBlank用法及代码示例

本文方法及代码示例筛选整理自kotlinlang.org------Kotlin 2.1.20 Released

ifBlank 所在包 kotlin.text.ifBlank,其相关用法介绍如下:

用法:

kotlin 复制代码
inline fun <C, R> C.ifBlank(
    defaultValue: () -> R
): R where C : CharSequence, C : R
  • 如果此字符序列不为空且不仅包含空白字符,则返回此字符序列,否则返回调用 defaultValue 函数的结果。

代码示例:

kotlin 复制代码
import java.util.Locale
import kotlin.test.*

fun main(args: Array<String>) {
    //sampleStart
    val blank = "    "

    val blankOrNull: String? = blank.ifBlank { null }
    println(blankOrNull) // null

    val blankOrDefault = blank.ifBlank { "default" }
    println(blankOrDefault) // default

    val nonBlank = "abc"
    val sameString = nonBlank.ifBlank { "def" }
    println("nonBlank === sameString is ${nonBlank === sameString}") // true
    //sampleEnd
}

// 输出 
null
default
nonBlank === sameString is true
相关推荐
奥尔特星云大使7 小时前
MySQL 慢查询日志slow query log
android·数据库·mysql·adb·慢日志·slow query log
2501_9159184111 小时前
iOS 框架全解析,原生框架与跨平台框架对比、开发应用打包与 App Store 上架实战经验
android·ios·小程序·https·uni-app·iphone·webview
K24B;11 小时前
多模态大语言模型LISA++
android·人工智能·语言模型·分割·多模态大语言模型
molihuan17 小时前
开源 全平台 哔哩哔哩缓存视频合并 Github地址:https://github.com/molihuan/hlbmerge_flutter
android·flutter·缓存·ffmpeg·开源·github·音视频
奶糖 肥晨17 小时前
批量重命名技巧:使用PowerShell一键整理图片文件命名规范
android·服务器·数据库
Momentary_SixthSense18 小时前
如何对较长的Stream链进行Debug
android·java·开发语言
little_fat_sheep18 小时前
【Rive】rive-android源码分析
android
教程分享大师20 小时前
新魔百和m401h全部版本当贝桌面固件卡刷包和线刷包带adb权限
android
rufeii20 小时前
网鼎杯 2020 青龙组
android
我要升天!21 小时前
MySQL表的内连和外连
android·mysql·adb