Best practice – how do I test for strings in Java?
•
Java
I render some data according to conditions Support method execution to check whether the string exists But I don't know if I can be satisfied with comparing null?
public boolean isString(MyClass var) { return null != var.getMyString(); }
Can we do better?
Solution
I've liked stringutils since Apache commons Lang isNotBlank():
StringUtils.isNotBlank(var.getMyString())
It performs an additional trim (), which is desirable in most cases If not, use stringutils isNotEmpty(). Another advantage: it uses charsequence, so you can pass string, StringBuilder, etc
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码