Java String isEmpty()

The Trieda Java String isEmpty() metóda kontroluje, či je vstupný reťazec prázdny alebo nie. Všimnite si, že tu prázdne znamená, že počet znakov obsiahnutých v reťazci je nula.

Podpis

Podpis alebo syntax metódy string isEmpty() je uvedená nižšie:

 public boolean isEmpty()  

Návraty

pravda, ak je dĺžka 0, inak je nepravda.

Od r

1.6

Interná implementácia

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

Príklad metódy Java String isEmpty().

Názov súboru: 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()); }}  
Vyskúšajte to

Výkon:

 true false  

Príklad metódy Java String isEmpty() 2

Názov súboru: StringIsEmptyExample2.java

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

Výkon:

 String s1 is empty Javatpoint  

Prázdne vs. Null Strings

Skôr v tomto návode sme diskutovali o tom, že prázdne reťazce obsahujú nula znakov. To isté však platí aj pre nulový reťazec. Nulový reťazec je reťazec, ktorý nemá žiadnu hodnotu.

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

Metóda isEmpty() nie je vhodná na kontrolu nulových reťazcov. Nasledujúci príklad ukazuje to isté.

Názov súboru: 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ýkon:

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

Tu môžeme použiť operátor == na kontrolu nulových reťazcov.

Názov súboru: 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ýkon:

 The string is null.  

Prázdne struny

Prázdne reťazce sú tie reťazce, ktoré obsahujú iba prázdne miesta. Metóda isEmpty() je veľmi užitočná na kontrolu prázdnych reťazcov. Zvážte nasledujúci príklad.

Názov súboru: 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ýkon:

 The string is blank. The string is not blank.