文章目录背景方法总览isNull只管有没有值isEmpty判断有没有内容isBlank最严格的那个连空格也不放过三个方法的对比一览什么场景用哪个完整 Demo 代码写在最后背景近期发现一款很有意思的HarmonyOS 三方库, 地址 pura/harmony-utils(V1.4.0) , 作者是桃花镇童长老, 我这里也是直接通过该作者公布的源码进行案例编写进行,写了到目前写了一部分demo ,感觉确实很有帮助,这里呢也是开始写一个系列的演示demo 供大家参考。如有帮助可以在OpenHarmony中进行下载安装进行使用哦案例demo导航展示↓↓↓↓↓↓接下来言归正传 ↓↓↓↓搞 HarmonyOS 开发字符串判空是绕不过去的坎。很多初学者写代码的时候遇到空值处理就习惯性地写if (str null || str undefined || str )这一长串。烦不烦HoUtils 里的StrUtil提供了三个判空方法名字看着像实际行为差别挺大。今天就把这三兄弟说清楚。方法总览isNull只管有没有值isNull只判断一件事这个变量是不是null或者undefined。注意它不管字符串内容哪怕你传个空字符串它也返回false因为空字符串是一个合法的值不是 null。// 点击按钮触发直接从 Demo 日志中对应this.addLog(isNull(${this.inputStr}) →${StrUtil.isNull(this.inputStr)});this.addLog(isNull(null) →${StrUtil.isNull(null)});this.addLog(isNull(undefined) →${StrUtil.isNull(undefined)});实际运行结果isNull( Hello HarmonyOS )→false有值不是 nullisNull(null)→true就是 nullisNull(undefined)→trueundefined 也算对应的isNotNull就是取反传进去不是 null/undefined 就返回 true。this.addLog(isNotNull(${this.inputStr}) →${StrUtil.isNotNull(this.inputStr)});isEmpty判断有没有内容isEmpty比isNull严格一点它不只检查 null还检查字符串是否为空字符串。但注意isEmpty不处理空白字符。你传一个全是空格的字符串 它也会返回false因为那个字符串有内容虽然是空格。this.addLog(isEmpty(${this.inputStr}) →${StrUtil.isEmpty(this.inputStr)});this.addLog(isEmpty() →${StrUtil.isEmpty()});实际运行结果isEmpty( Hello HarmonyOS )→false有内容isEmpty()→true空字符串isEmpty(null)→ 很多实现里也返回true因为没有内容对应的isNotEmpty就是非空才返回 truethis.addLog(isNotEmpty(${this.inputStr}) →${StrUtil.isNotEmpty(this.inputStr)});isBlank最严格的那个连空格也不放过isBlank是三兄弟里最挑剔的。它不只检查 null 和空字符串还会检查字符串是否全为空白字符空格、制表符等。this.addLog(isBlank(${this.inputStr}) →${StrUtil.isBlank(this.inputStr)});this.addLog(isBlank( ) →${StrUtil.isBlank( )});实际运行结果isBlank( Hello HarmonyOS )→false虽然有空格但有实际内容isBlank( )→true全是空格当空白处理isBlank()→trueisBlank(null)→true对应的isNotBlank就是真正有可见内容才返回 truethis.addLog(isNotBlank(${this.inputStr}) →${StrUtil.isNotBlank(this.inputStr)});三个方法的对比一览场景isNullisEmptyisBlanknull✅ true✅ true✅ trueundefined✅ true✅ true✅ true空字符串❌ false✅ true✅ true 纯空格❌ false❌ false✅ truehello正常字符串❌ false❌ false❌ false什么场景用哪个用isNull你只想区分有没有传参null 和 undefined 需要特殊处理但空字符串是合法值的场景。比如接口返回值检查。用isEmpty你需要确保字符串有内容但空白字符对你来说也有意义的场景。比如密码字段——密码是空格也算有输入。用isBlank表单验证场景。用户只输入了空格你肯定想拦截这时候用isBlank最合适。比如用户名输入框、搜索框。完整 Demo 代码完整的 Demo 页面代码如下可以直接在 HarmonyOS 项目中运行importrouterfromohos.router;import{StrUtil}from../Utils/StrUtil;interfaceLogItem{time:string;text:string;}EntryComponentstruct StrUtilDemoPage{Statelogs:LogItem[][];StateinputStr:string Hello HarmonyOS ;privateaddLog(text:string){constdnewDate();consttime${d.getHours().toString().padStart(2,0)}:${d.getMinutes().toString().padStart(2,0)}:${d.getSeconds().toString().padStart(2,0)};constnewItem:LogItem{time,text};this.logs([newItem]asLogItem[]).concat(this.logs).slice(0,60);}build(){Column(){// isNull / isNotNullButton(isNull(null)).onClick((){this.addLog(isNull(null) →${StrUtil.isNull(null)});})Button(isNull(undefined)).onClick((){this.addLog(isNull(undefined) →${StrUtil.isNull(undefined)});})Button(isEmpty()).onClick((){this.addLog(isEmpty() →${StrUtil.isEmpty()});})Button(isBlank( ) 全空格).onClick((){this.addLog(isBlank( ) →${StrUtil.isBlank( )});})Button(isNotBlank(hello)).onClick((){this.addLog(isNotBlank(hello) →${StrUtil.isNotBlank(hello)});})}}}写在最后这三个方法说起来简单但真正搞清楚差别能帮你少踩很多坑。最常见的错误就是在表单验证里用了isEmpty结果用户输入一串空格也通过了检查。改成isBlank就能正确拦截。下一篇我们聊trim和trimAll——去空格也有讲究。
HarmonyOS StrUtil 字符串判空三兄弟:isNull、isEmpty、isBlank 到底有啥区别?
发布时间:2026/5/30 11:38:59
文章目录背景方法总览isNull只管有没有值isEmpty判断有没有内容isBlank最严格的那个连空格也不放过三个方法的对比一览什么场景用哪个完整 Demo 代码写在最后背景近期发现一款很有意思的HarmonyOS 三方库, 地址 pura/harmony-utils(V1.4.0) , 作者是桃花镇童长老, 我这里也是直接通过该作者公布的源码进行案例编写进行,写了到目前写了一部分demo ,感觉确实很有帮助,这里呢也是开始写一个系列的演示demo 供大家参考。如有帮助可以在OpenHarmony中进行下载安装进行使用哦案例demo导航展示↓↓↓↓↓↓接下来言归正传 ↓↓↓↓搞 HarmonyOS 开发字符串判空是绕不过去的坎。很多初学者写代码的时候遇到空值处理就习惯性地写if (str null || str undefined || str )这一长串。烦不烦HoUtils 里的StrUtil提供了三个判空方法名字看着像实际行为差别挺大。今天就把这三兄弟说清楚。方法总览isNull只管有没有值isNull只判断一件事这个变量是不是null或者undefined。注意它不管字符串内容哪怕你传个空字符串它也返回false因为空字符串是一个合法的值不是 null。// 点击按钮触发直接从 Demo 日志中对应this.addLog(isNull(${this.inputStr}) →${StrUtil.isNull(this.inputStr)});this.addLog(isNull(null) →${StrUtil.isNull(null)});this.addLog(isNull(undefined) →${StrUtil.isNull(undefined)});实际运行结果isNull( Hello HarmonyOS )→false有值不是 nullisNull(null)→true就是 nullisNull(undefined)→trueundefined 也算对应的isNotNull就是取反传进去不是 null/undefined 就返回 true。this.addLog(isNotNull(${this.inputStr}) →${StrUtil.isNotNull(this.inputStr)});isEmpty判断有没有内容isEmpty比isNull严格一点它不只检查 null还检查字符串是否为空字符串。但注意isEmpty不处理空白字符。你传一个全是空格的字符串 它也会返回false因为那个字符串有内容虽然是空格。this.addLog(isEmpty(${this.inputStr}) →${StrUtil.isEmpty(this.inputStr)});this.addLog(isEmpty() →${StrUtil.isEmpty()});实际运行结果isEmpty( Hello HarmonyOS )→false有内容isEmpty()→true空字符串isEmpty(null)→ 很多实现里也返回true因为没有内容对应的isNotEmpty就是非空才返回 truethis.addLog(isNotEmpty(${this.inputStr}) →${StrUtil.isNotEmpty(this.inputStr)});isBlank最严格的那个连空格也不放过isBlank是三兄弟里最挑剔的。它不只检查 null 和空字符串还会检查字符串是否全为空白字符空格、制表符等。this.addLog(isBlank(${this.inputStr}) →${StrUtil.isBlank(this.inputStr)});this.addLog(isBlank( ) →${StrUtil.isBlank( )});实际运行结果isBlank( Hello HarmonyOS )→false虽然有空格但有实际内容isBlank( )→true全是空格当空白处理isBlank()→trueisBlank(null)→true对应的isNotBlank就是真正有可见内容才返回 truethis.addLog(isNotBlank(${this.inputStr}) →${StrUtil.isNotBlank(this.inputStr)});三个方法的对比一览场景isNullisEmptyisBlanknull✅ true✅ true✅ trueundefined✅ true✅ true✅ true空字符串❌ false✅ true✅ true 纯空格❌ false❌ false✅ truehello正常字符串❌ false❌ false❌ false什么场景用哪个用isNull你只想区分有没有传参null 和 undefined 需要特殊处理但空字符串是合法值的场景。比如接口返回值检查。用isEmpty你需要确保字符串有内容但空白字符对你来说也有意义的场景。比如密码字段——密码是空格也算有输入。用isBlank表单验证场景。用户只输入了空格你肯定想拦截这时候用isBlank最合适。比如用户名输入框、搜索框。完整 Demo 代码完整的 Demo 页面代码如下可以直接在 HarmonyOS 项目中运行importrouterfromohos.router;import{StrUtil}from../Utils/StrUtil;interfaceLogItem{time:string;text:string;}EntryComponentstruct StrUtilDemoPage{Statelogs:LogItem[][];StateinputStr:string Hello HarmonyOS ;privateaddLog(text:string){constdnewDate();consttime${d.getHours().toString().padStart(2,0)}:${d.getMinutes().toString().padStart(2,0)}:${d.getSeconds().toString().padStart(2,0)};constnewItem:LogItem{time,text};this.logs([newItem]asLogItem[]).concat(this.logs).slice(0,60);}build(){Column(){// isNull / isNotNullButton(isNull(null)).onClick((){this.addLog(isNull(null) →${StrUtil.isNull(null)});})Button(isNull(undefined)).onClick((){this.addLog(isNull(undefined) →${StrUtil.isNull(undefined)});})Button(isEmpty()).onClick((){this.addLog(isEmpty() →${StrUtil.isEmpty()});})Button(isBlank( ) 全空格).onClick((){this.addLog(isBlank( ) →${StrUtil.isBlank( )});})Button(isNotBlank(hello)).onClick((){this.addLog(isNotBlank(hello) →${StrUtil.isNotBlank(hello)});})}}}写在最后这三个方法说起来简单但真正搞清楚差别能帮你少踩很多坑。最常见的错误就是在表单验证里用了isEmpty结果用户输入一串空格也通过了检查。改成isBlank就能正确拦截。下一篇我们聊trim和trimAll——去空格也有讲究。