Java String isBlank() 方法详解

String.isBlank() 方法简介

String.isBlank() 方法是在 Java 11 中引入的。它主要用于检查一个字符串是否为空,或者是否仅包含空白字符(例如空格、制表符或换行符)。这个方法大大简化了字符串验证的过程。

示例 1:基础用法

在这个示例中,让我们使用基础的方法,通过 isBlank()检查给定的字符串是否为空(即空字符串或仅包含空白字符)。

// Java program to demonstrate isBlank() method
public class StringIsBlank {
  
    public static void main(String[] args) {
      
        // Declare and initialize strings
        String s1 = "   ";    // String with only spaces
        String s2 = "Hello, World!";    // Non-empty string

        // Check if the strings are blank
        System.out.println(s1.isBlank());  
        System.out.println(s2.isBlank());  
    }
}

输出

true
false

isBlank() 方法的语法

> boolean isBlank()

返回类型:

  • 如果字符串为空或仅包含空白字符,则返回 true
  • 如果字符串包含任何非空白字符,则返回 false

示例 2:区分空字符串与空白字符串

这个示例演示了如何区分空字符串 ("") 和仅包含空白字符的字符串

// Java program to distinguish between 
// empty and blank strings
public class StringIsBlank {
    public static void main(String[] args) {
      
        // Array of strings
        String[] s = {"", "   ", "Hello", " \t
"};  

        // Loop through the array of strings
        for (int i = 0; i < s.length; i++) {
          
            // Check if the string is blank
            if (s[i].isBlank()) {
              
                System.out.println("String at index " + i 
                                   + " is blank (contains only whitespace).");
            } else {
                System.out.println("String at index " + i 
                                   + " is not blank.");
            }
        }
    }
}

输出

String at index 0 is blank (contains only whitespace).
String at index 1 is blank (contains only whitespace).
String at index 2 is not blank.
String at index 3 is blank (contains only whitespace).

示例 3:结合条件逻辑处理输入

在这个示例中,让我们看看如何将 isBlank()条件逻辑结合使用,以处理应用程序中的空白输入。

// Java program to use isBlank() with conditional logic
public class StringIsBlank {
    public static void main(String[] args) {
      
        // String with spaces
        String s = "   ";  

        // Check if the message is blank
        if (s.isBlank()) {

            System.out.println("The message is blank.");
        } else {
            System.out.println("Message: " + s);
        }
    }
}

输出

The message is blank.

isBlank() 与其他方法进行比较

在 Java 11 之前,开发人员通常使用 INLINECODE9854cee1,或者结合使用 INLINECODE262e5641 和 INLINECODEa4395d59 来检查字符串是否为空白。然而,INLINECODE9d0c621d 提供了一种更简单、更高效的解决方案,因为它只需一次调用即可自动处理空字符串和仅包含空白字符的字符串。

  • isEmpty(): 检查字符串长度是否为 0,但不考虑仅包含空白字符的字符串。
  • trim().isEmpty(): 去除首尾空白字符后,检查结果字符串是否为空。
  • isBlank(): 检查字符串是否为空,或者是否完全由空白字符组成。

示例:

public class CompareMethods {
  
    public static void main(String[] args) {
      
        // Only whitespace
        String s = "   ";  

        // Using isEmpty
        System.out.println(s.isEmpty()); 

        // Using trim() + isEmpty
        System.out.println(s.trim().isEmpty());  

        // Using isBlank
        System.out.println(s.isBlank());  
    }
}

输出

false
true
true
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。如需转载,请注明文章出处豆丁博客和来源网址。https://shluqu.cn/25637.html
点赞
0.00 平均评分 (0% 分数) - 0