ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

处理空输入踩的坑

2026/8/16 1:44:36 拓冰建站 浏览量
处理空输入踩的坑

处理一个外部传入的用户输入的时候,一开始只想着处理null,用"?.let{}"的模式处理。结果发现结果异常。
打断点发现传入值不是空,而是""
由此发现kotlin处理空string的两个函数:

isEmpty()

The isEmpty() function returns true only if the string has a length of zero.

  • "" is empty.
  • " " is not empty.
  • "\t\n" is not empty.
  • "hello" is not empty.

Essentially, isEmpty() performs a very literal check: are there any characters in this string at all?

isBlank()

The isBlank() function is more nuanced. It returns true if the string is empty OR if it consists only of whitespace characters (spaces, tabs, newlines, etc.).

  • "" is blank.
  • " " is blank.
  • "\t\n" is blank.
  • "hello" is not blank.

isBlank() checks if there is any meaningful, non-whitespace content in the string.