简体   繁体   English

检查字符串是否为空的更好方法是在 Java 5/6 中使用 String.trim().length() 吗?

[英]What's the better way to check if a String is empty than using String.trim().length() in Java 5/6?

There probably is a method to return the index of the first non-blank char in a String in Java5/6.在 Java5/6 中可能有一种方法可以返回字符串中第一个非空白字符的索引。 But I cannot find it any more.但我再也找不到它了。 A code anylizing tool says it is better than checking String.trim().length() .代码分析工具说它比检查String.trim().length()更好。

I always like to use the Apache Commons StringUtils library.我总是喜欢使用 Apache Commons StringUtils 库。 It has isEmpty() and is isBlank() which handles whitespace.它有 isEmpty() 和 isBlank() 处理空白。

http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringUtils.html http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringUtils.html

Not to mention the numerous other helpful methods in that class and the library in general.更不用说 class 和一般库中的许多其他有用的方法。

I'd use the Guava CharMatcher class:我会使用番石榴CharMatcher class:

boolean onlyWhitespace = CharMatcher.WHITESPACE.matchesAllOf(input);

Okay guys, I have found it finally from PMD rules of InefficientEmptyStringCheck :好的,我终于从InefficientEmptyStringCheck的 PMD 规则中找到了它:

InefficientEmptyStringCheck:低效的EmptyStringCheck:
Since: PMD 3.6自:PMD 3.6
String.trim().length() is an inefficient way to check if a String is really empty, as it creates a new String object just to check its size. String.trim().length() 是一种检查字符串是否真的为空的低效方法,因为它创建一个新的字符串 object 只是为了检查它的大小。 Consider creating a static function that loops through a string, checking Character.isWhitespace() on each character and returning false if a non-whitespace character is found.考虑创建一个循环字符串的 static function,检查每个字符的 Character.isWhitespace() 并在找到非空白字符时返回 false。

This is only a suggestion from PMD.这只是 PMD 的建议。 To adopt it or not is depending on which has priority: the efficiency of programs or the time of programmers.采用与否取决于哪个优先:程序的效率或程序员的时间。

Java 11 introduces the "isBlank" method. Java 11 介绍了“isBlank”方法。 See https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#isBlank()请参阅https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#isBlank()

Returns true if the string is empty or contains only white space codepoints, otherwise false.如果字符串为空或仅包含空白代码点,则返回 true,否则返回 false。

Java 6 has introduced String.isEmpty() , so you could use it in conjunction with String.trim() . Java 6 引入了String.isEmpty() ,因此您可以将它与String.trim()结合使用。 You can also use regular expressions, for example using such a condition: .str.matches("\\s*") .您还可以使用正则表达式,例如使用这样的条件: .str.matches("\\s*")

If you want to test, whether it only contains whitespace characters, you can use RegEx如果要测试,是否只包含空格字符,可以使用RegEx

string.matches("\\s*")

Thinks it's more efficient than trim().isEmpty(), especially if you expect whitespaces and have long Strings, though I'm not sure how much effort it takes to compile the RegEx.认为它比 trim().isEmpty() 更有效,特别是如果您期望空格并且有长字符串,但我不确定编译 RegEx 需要多少努力。

If you want to test for a string that has a zero length than using isEmpty() or length() == 0 is the best way.如果要测试长度为零的字符串,则最好使用isEmpty()length() == 0

If you want to test if the string only contains whitespaces, then searching for the first non-whitespace character is more efficient because not intermediate object is created (as with trim() )如果要测试字符串是否仅包含空格,则搜索第一个非空格字符会更有效,因为不会创建中间 object(与trim()一样)

But in any case I too recommend Apache's commons StringUtils.isEmpty() as it nicely encapsulates all this.但无论如何,我也推荐 Apache 的 commons StringUtils.isEmpty() ,因为它很好地封装了所有这些。

There is a method in String for this exact purpose. String 中有一个用于此目的的方法。

String emptyString = "";
emptyString.isEmpty();

This will return true.这将返回 true。

try尝试

  str!=null && str.trim().isEmpty()

(Best way) string.equals("") (最佳方式) string.equals("")

But also,但是也,

string.isEmpty()

string.equals(null)

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

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