溫馨提示×

java怎么判斷字符串是否為空

小億
101
2023-12-26 19:37:57
欄目: 編程語言

在Java中,可以使用以下方法來判斷字符串是否為空:

  1. 使用length()方法,判斷字符串的長度是否為0:
String str = "example";
if (str.length() == 0) {
    System.out.println("字符串為空");
} else {
    System.out.println("字符串不為空");
}
  1. 使用isEmpty()方法,判斷字符串是否為空字符串(長度為0):
String str = "example";
if (str.isEmpty()) {
    System.out.println("字符串為空");
} else {
    System.out.println("字符串不為空");
}
  1. 使用isBlank()方法,判斷字符串是否為空或只包含空格:
String str = "example";
if (str.isBlank()) {
    System.out.println("字符串為空");
} else {
    System.out.println("字符串不為空");
}

其中,isBlank()方法是在Java 11中引入的,如果使用的是Java 11或更高版本,建議使用該方法來判斷字符串是否為空。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女