与在 Java 5/6 中使用 String.trim().length() 相比,检查 String 是否为空的更好方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5819800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the better way to check if a String is empty than using String.trim().length() in Java 5/6?
提问by chance
There probably is a method to return the index of the first non-blank char in a String in Java5/6. But I cannot find it any more. A code anylizing tool says it is better than checking String.trim().length()
.
可能有一种方法可以在 Java5/6 中返回 String 中第一个非空字符的索引。但我再也找不到它了。一个代码任意化工具说它比检查更好String.trim().length()
。
采纳答案by Joachim Sauer
I'd use the GuavaCharMatcher
class:
我会使用番石榴CharMatcher
类:
boolean onlyWhitespace = CharMatcher.WHITESPACE.matchesAllOf(input);
回答by Jberg
I always like to use the Apache Commons StringUtils library. It has isEmpty() and is isBlank() which handles whitespace.
我总是喜欢使用 Apache Commons StringUtils 库。它有 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.
更不用说该类和整个库中的许多其他有用的方法。
回答by chance
Okay guys, I have found it finally from PMD rules of InefficientEmptyStringCheck:
好球员,我终于找到了从PMD规则InefficientEmptyStringCheck:
InefficientEmptyStringCheck:
Since: 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. 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.
InefficientEmptyStringCheck:
因为:PMD 3.6
String.trim().length() 是一种检查 String 是否真的为空的低效方法,因为它创建一个新的 String 对象只是为了检查其大小。考虑创建一个循环遍历字符串的静态函数,检查每个字符上的 Character.isWhitespace() 并在找到非空白字符时返回 false。
This is only a suggestion from PMD. To adopt it or not is depending on which has priority: the efficiency of programs or the time of programmers.
这只是来自 PMD 的建议。采用与否取决于哪个优先:程序的效率或程序员的时间。
回答by David Kerr
Java 11 introduces the "isBlank" method. See https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#isBlank()
Java 11 引入了“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。
回答by Bolo
Java 6 has introduced String.isEmpty()
, so you could use it in conjunction with String.trim()
. You can also use regular expressions, for example using such a condition: !str.matches("\\s*")
.
Java 6 已经引入String.isEmpty()
,因此您可以将它与String.trim()
. 您还可以使用正则表达式,例如使用这样的条件:!str.matches("\\s*")
。
回答by Tobber
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 需要多少努力。
回答by a_horse_with_no_name
If you want to test for a string that has a zero length than using isEmpty()
or length() == 0
is the best way.
如果要测试长度为零的字符串,则使用isEmpty()
orlength() == 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()
)
如果要测试字符串是否仅包含空格,则搜索第一个非空格字符会更有效,因为不会创建中间对象(如trim()
)
But in any case I too recommend Apache's commons StringUtils.isEmpty()
as it nicely encapsulates all this.
但无论如何,我也推荐 Apache 的 commons,StringUtils.isEmpty()
因为它很好地封装了所有这些。
回答by Zach
There is a method in String for this exact purpose.
String 中有一个用于此确切目的的方法。
String emptyString = "";
emptyString.isEmpty();
This will return true.
这将返回true。
回答by Md Ayub Ali Sarker
try
尝试
str!=null && str.trim().isEmpty()
回答by Christopher Gwilliams
(Best way) string.equals("")
(最好的办法) string.equals("")
But also,
但是也,
string.isEmpty()
string.equals(null)