英文:
String.isBlank() replacement
问题
我正在尝试适应使用String.isBlank(text)
的代码。这个方法是在Java 11中引入的,但我的当前项目使用的是Java 8,我不想升级。是否有isBlank
方法的源代码或如何替换它的地方?
我尝试创建一个方法,但不确定应该在其中放置什么检查:
/**
* @param text - 要检查的文本
* @return 如果{@code text}为空或仅包含空白代码点,则返回{@code true}
*/
public static final boolean isBlank(final String text)
{
if (text == null || text.isEmpty())
{
return true;
}
for (final char c : text.toCharArray())
{
// 在这里进行检查
}
return false;
}
英文:
I'm trying to adapt a code which uses String.isBlank(text)
. This method was introduced in Java 11, but my current project uses Java 8 and I don't want to upgrade. Is there anywhere the source code of the isBlank
method or how can I replace it?
I tried make a method but I'm not sure what check should I put in there:
/**
* @param text - the text to check
* @return {@code true} if {@code text} is empty or contains only white space codepoints
*/
public static final boolean isBlank(final String text)
{
if (text == null || text.isEmpty())
{
return true;
}
for (final char c : text.toCharArray())
{
//Check here
}
return false;
}
答案1
得分: 5
在类 org.apache.commons.lang3.StringUtils 中,函数isBlank如下:
public static boolean isBlank(final CharSequence cs) {
int strLen;
if (cs == null || (strLen = cs.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(cs.charAt(i))) {
return false;
}
}
return true;
}
它在Java 8中运行良好,只需要导入它即可。
英文:
In the class org.apache.commons.lang3.StringUtils, the function isBlank is like this:
public static boolean isBlank(final CharSequence cs) {
int strLen;
if (cs == null || (strLen = cs.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(cs.charAt(i))) {
return false;
}
}
return true;
}
It works well for Java 8, you just have to import it
答案2
得分: 3
Before Java 11: string.trim().isEmpty();
so, if you want wrap isBlank function, you can do as follow:
public static final boolean isBlank(final String text) {
return text == null || text.trim().isEmpty();
}
英文:
Before Java 11: string.trim().isEmpty();
so, if you want wrap isBlank function, you can do as follow:
public static final boolean isBlank(final String text) {
return text == null || text.trim().isEmpty();
}
答案3
得分: 1
我遇到了与您相同的问题,一个Java 11项目,我不得不降级到Java 8。您可以在您的项目中导入Apache Commons:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
然后您只需调用 StringUtils.isBlank(string) 它将返回true或false。
英文:
I had the same issue that you, a Java 11 project which I had to downgrade to Java 8. You can import Apache Commons in your project:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
Then only what you need to do is call StringUtils.isBlank(string) it will return true or false
答案4
得分: 0
你可以使用Guava库,并使用Strings.nullToEmpty(str).trim().isEmpty()
来复制isBlank
的行为。
英文:
You can use Guava library, and
Strings.nullToEmpty(str).trim().isEmpty()
to replicate the isBlank behaviour.
答案5
得分: 0
除了其他答案(导入库),您可以在StringUtils类中定义自己的函数,然后可以添加任何其他字符串工具操作。
您想要的当前方法将如下所示:
public static boolean isNullOrEmpty(String text) {
boolean isNull = true;
if (text != null) {
if (!text.trim().equals("")) {
isNull = false;
}
}
return isNull;
}
此外,通过这种方式,您不需要导入任何库。
英文:
In addition to other answers (importing libraries), you can define your own function in a StringUtils class, then you can add any other String utils operations.
The current method you want would look like this:
public static boolean isNullOrEmpty(String text) {
boolean isNull = true;
if (text != null) {
if (!text.trim().equals("")) {
isNull = false;
}
}
return isNull;
}
Also, through this you don't have to import any library
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论