本文摘自:http://blog.farifam.com/2018/01/28/kotlin-charsequence-isnullorblank-vs-isnullorempty/

Koltin provide two options to check if a CharSequence or String have a null or empty value, IsNullOrBlank & isNullOrEmpty. What’s the difference? let’s check it by code:

var data: String? = null
println(data.isNullOrBlank()?.toString())  // output: true
println(data.isNullOrEmpty()?.toString())  // output: true

it return same result. How about enter blank string on it:

data = ""  
println(data.isNullOrBlank()?.toString())  //output: true
println(data.isNullOrEmpty()?.toString())  //output: true

Hmm, it return same result too.. How about enter it with some text?

data = "hello"
println(data.isNullOrBlank()?.toString())  //output: false
println(data.isNullOrEmpty()?.toString())  //output: false

Yes, of course they all return “false”. How about enter blank text on it?

data = " " // this is a text with blank space
println(data.isNullOrBlank()?.toString())  //true
println(data.isNullOrEmpty()?.toString())  //false

Yeah there’s the different..

Happy coding..

相关文章:

  • 2021-07-10
  • 2021-11-29
  • 2022-12-23
  • 2021-12-04
  • 2021-07-28
  • 2022-01-15
  • 2021-12-04
猜你喜欢
  • 2021-08-25
  • 2021-10-13
  • 2022-12-23
  • 2022-12-23
  • 2022-02-27
  • 2022-12-23
  • 2021-06-11
相关资源
相似解决方案