Android 在kotlin中isEmpty和isBlank有什么不同

Android 在kotlin中isEmpty和isBlank有什么不同,android,kotlin,Android,Kotlin,我想在我的in-android项目中检查字符串的值,所以我看到了两个字符串值函数: item.isBlank() 及 它们之间有什么区别?item.isEmpty()只检查字符串的长度 item.isBlank() 也就是说 “”.isEmpty()应返回false “”.isBlank()应返回true 从isBlank的文档中 如果此字符串为空或仅由空格组成,则返回true 人物 以后,您可以阅读文档并查看IDE中的代码,只需单击Ctrl+B或Command+B。这是在documant

我想在我的in-android项目中检查字符串的值,所以我看到了两个字符串值函数:

item.isBlank()

它们之间有什么区别?

item.isEmpty()
只检查字符串的长度

item.isBlank()

也就是说

  • “”.isEmpty()
    应返回false
  • “”.isBlank()
    应返回true
isBlank的文档中

如果此字符串为空或仅由空格组成,则返回true 人物


以后,您可以阅读文档并查看IDE中的代码,只需单击Ctrl+B或Command+B。这是在documantation for isEmpty方法中编写的:

/**
 * Returns `true` if this char sequence is empty (contains no characters).
 *
 * @sample samples.text.Strings.stringIsEmpty
 */
@kotlin.internal.InlineOnly
public inline fun CharSequence.isEmpty(): Boolean = length == 0
对于isBlank:

 * Returns `true` if this string is empty or consists solely of whitespace characters.
 *
 * @sample samples.text.Strings.stringIsBlank
 */
public actual fun CharSequence.isBlank(): Boolean = length == 0 || indices.all { this[it].isWhitespace() }

这回答了你的问题吗@xszym这是一个针对不同类的Java问题(当然是相同内容)。
 * Returns `true` if this string is empty or consists solely of whitespace characters.
 *
 * @sample samples.text.Strings.stringIsBlank
 */
public actual fun CharSequence.isBlank(): Boolean = length == 0 || indices.all { this[it].isWhitespace() }