`String.isBlank()` 替代方法

huangapple go评论66阅读模式
英文:

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 &lt; 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:

&lt;dependency&gt;
    &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
    &lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;
    &lt;version&gt;3.9&lt;/version&gt;
&lt;/dependency&gt;

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(&quot;&quot;)) {
                isNull = false;
            }
        }
        return isNull;
    }

Also, through this you don't have to import any library

huangapple
  • 本文由 发表于 2020年8月12日 19:50:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63375935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定