Java String isEmpty()

The Třída Java String isEmpty() metoda kontroluje, zda je vstupní řetězec prázdný nebo ne. Všimněte si, že zde prázdné znamená, že počet znaků obsažených v řetězci je nula.

Podpis

Podpis nebo syntaxe metody string isEmpty() je uvedena níže:

 public boolean isEmpty()  

Návraty

true, pokud je délka 0, jinak nepravda.

Od té doby

1.6

Vnitřní implementace

 public boolean isEmpty() { return value.length == 0; }  

Příklad metody Java String isEmpty().

Název souboru: StringIsEmptyExample.java

 public class IsEmptyExample{ public static void main(String args[]){ String s1=''; String s2='javatpoint'; System.out.println(s1.isEmpty()); System.out.println(s2.isEmpty()); }}  
Otestujte to hned

Výstup:

 true false  

Příklad metody Java String isEmpty() 2

Název souboru: StringIsEmptyExample2.java

 public class IsEmptyExample2 { public static void main(String[] args) }  

Výstup:

 String s1 is empty Javatpoint  

Prázdný vs. Nulové řetězce

Dříve v tomto tutoriálu jsme diskutovali o tom, že prázdné řetězce obsahují nula znaků. Totéž však platí i pro nulový řetězec. Nulový řetězec je řetězec, který nemá žádnou hodnotu.

 String str = ''; // empty string String str1 = null; // null string. It is also not containing any characters.  

Metoda isEmpty() není vhodná pro kontrolu prázdných řetězců. Následující příklad ukazuje totéž.

Název souboru: StringIsEmptyExample3.java

 public class StringIsEmptyExample3 { // main method public static void main(String argvs[]) { String str = null; if(str.isEmpty()) { System.out.println('The string is null.'); } else { System.out.println('The string is not null.'); } } }  

Výstup:

 Exception in thread 'main' java.lang.NullPointerException at StringIsEmptyExample3.main(StringIsEmptyExample3.java:7)  

Zde můžeme použít operátor == ke kontrole nulových řetězců.

Název souboru: StringIsEmptyExample4.java

 class StringIsEmptyExample4 { // main method public static void main(String argvs[]) { String str = null; if(str == null) { System.out.println('The string is null.'); } else { System.out.println('The string is not null.'); } } }  

Výstup:

 The string is null.  

Prázdné struny

Prázdné řetězce jsou řetězce, které obsahují pouze mezery. Metoda isEmpty() je velmi užitečná pro kontrolu prázdných řetězců. Zvažte následující příklad.

Název souboru: StringIsEmptyExample5.java

 public class StringIsEmptyExample5 { // main method public static void main(String argvs[]) { // a blank string String str = ' '; int size = str.length(); // trim the white spaces and after that // if the string results in the empty string // then the string is blank; otherwise, not. if(size == 0) { System.out.println('The string is empty. 
'); } else if(size > 0 && str.trim().isEmpty()) { System.out.println('The string is blank. 
'); } else { System.out.println('The string is not blank. 
'); } str = ' Welcome to JavaTpoint. '; size = str.length(); if(size == 0) { System.out.println('The string is empty. 
'); } if(size > 0 && str.trim().isEmpty()) { System.out.println('The string is blank. 
'); } else { System.out.println('The string is not blank. 
'); } } }  

Výstup:

 The string is blank. The string is not blank.