logo

מחרוזת Java isEmpty()

ה מחלקת Java String isEmpty() השיטה בודקת אם מחרוזת הקלט ריקה או לא. שימו לב שכאן ריק פירושו שמספר התווים הכלולים במחרוזת הוא אפס.

חֲתִימָה

החתימה או התחביר של שיטת המחרוזת isEmpty() ניתנים להלן:

דפוסי תוכנת java
 public boolean isEmpty() 

החזרות

נכון אם האורך הוא 0 אחרת שקר.

מאז

1.6

יישום פנימי

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

דוגמה לשיטת Java String isEmpty()‎

שם קובץ: 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()); }} 
בדוק את זה עכשיו

תְפוּקָה:

 true false 

שיטה Java String isEmpty() דוגמה 2

שם קובץ: StringIsEmptyExample2.java

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

תְפוּקָה:

 String s1 is empty Javatpoint 

ריק לעומת מחרוזות אפס

מוקדם יותר במדריך זה, דנו בכך שהמחרוזות הריקות מכילות אפס תווים. עם זאת, הדבר נכון גם עבור מחרוזת ריק. מחרוזת null היא מחרוזת שאין לה ערך.

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

השיטה isEmpty() אינה מתאימה לבדיקת מחרוזות null. הדוגמה הבאה מציגה את אותו הדבר.

שם קובץ: 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.'); } } } 

תְפוּקָה:

אלגוריתם תזמון עגול רובין
 Exception in thread 'main' java.lang.NullPointerException at StringIsEmptyExample3.main(StringIsEmptyExample3.java:7) 

כאן, נוכל להשתמש באופרטור == כדי לבדוק את מחרוזות האפס.

שם קובץ: 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.'); } } } 

תְפוּקָה:

 The string is null. 

מיתרים ריקים

מחרוזות ריקות הן מחרוזות המכילות רק רווחים לבנים. השיטה isEmpty() שימושית מאוד כדי לבדוק את המחרוזות הריקות. שקול את הדוגמה הבאה.

עומס יתר בשיטה

שם קובץ: 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. 
'); } } } 

תְפוּקָה:

 The string is blank. The string is not blank.