简体   繁体   English

Apache的StringUtils.isBlank(str)与Guava的Strings.isNullOrEmpty(str):你应该经常检查空格吗?

[英]Apache's StringUtils.isBlank(str) vs. Guava's Strings.isNullOrEmpty(str): Should you routinely check for whitespace?

Is there any advantage in using 使用是否有任何优势

StringUtils.isBlank(str) 

from Apache commons-lang. 来自Apache commons-lang。

vs VS

Strings.isNullOrEmpty(String string)

from Google Guava? 来自Google Guava?

I want to replace hundreds of cases of they following usage in a Java project: 我想在Java项目中使用后替换数百个它们:

if(str == null || str.isEmpty())

Guava's isNullOrEmpty seems to be a direct replacement for the usage above in my project. Guava的isNullOrEmpty似乎是我项目中上述用法的直接替代品。

But more people seem to use Apache's isBlank method based on my reading of SO questions. 但是,基于我对SO问题的阅读,更多人似乎使用了Apache的isBlank方法。

The only difference seems to be that StringUtils.isBlank(str) also checks for whitespace in addition to checking whether the string is null or empty. 唯一的区别似乎是StringUtils.isBlank(str)除了检查字符串是null还是空之外还检查空格

Normally is it a good idea to check a String for whitespace or could that produce a different result in your code than Guava's simpler check? 通常,检查字符串的空格是否是一个好主意,或者在代码中产生的结果可能比Guava更简单的检查更好?

如果你想使用Guava来复制isBlank行为,我会改用以下方法:

Strings.nullToEmpty(str).trim().isEmpty()

When you have to accept input from human beings, you should be forgiving and strip leading and trailing whitespace from whatever text they type, if it makes sense in the particular application. 当你必须接受来自人类的输入时,如果在特定的应用程序中有意义的话,你应该宽恕并从他们键入的任何文本中删除前导和尾随空格。

That said, using isBlank is only halfbaked. 也就是说,使用isBlank只是半熟的。 You also need to trim the strings before processing them further. 您还需要在进一步处理之前trim字符串。 So I suggest to use s = trim(s); 所以我建议使用s = trim(s); before checking with isNullOrEmpty . 在使用isNullOrEmpty进行检查之前。

StringUtils.isBlank(str) is very much different than Strings.isNullOrEmpty(String string) StringUtils.isBlank(str)Strings.isNullOrEmpty(String string)非常不同

the first code sample will only check if the string is empty or not, it will include white spaces as well and return true 第一个代码示例只检查字符串是否为空,它也将包含空格并返回true

StringUtils.isBlank(null)      = true 
StringUtils.isBlank("")         = true 
StringUtils.isBlank(" ")        = true 
StringUtils.isBlank("bob")      = false
StringUtils.isBlank("  bob  ")  = false

where as Strings.isNullOrEmpty(String string) Returns true if the given string is null or is the empty string where as Strings.isNullOrEmpty(String string)如果给定的字符串为null或为空字符串,则返回true

isBlank is incredibly overrated. isBlank被高估了。 UI code that reads user text straight out of input fields can trim whitespace once and for all, and then you can stop worrying about it. 直接从输入字段读取用户文本的UI代码可以一劳永逸地修剪空格,然后您可以不再担心它。

Guava is more or less targeted towards being a 'next generation' replacement of Apache Commons. 番石榴或多或少的目标是成为Apache Commons的“下一代”替代品。 There really isn't much practical difference between using isBlank() vs isNullOrEmpty() beyond using one or the other consistently. 除了使用一个或另一个之外,使用isBlank()和isNullOrEmpty()之间确实没有太大的实际区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM