logo

자바 문자열 isEmpty()

그만큼 Java 문자열 클래스 isEmpty() 메소드는 입력 문자열이 비어 있는지 확인합니다. 여기서 비어 있다는 것은 문자열에 포함된 문자 수가 0이라는 것을 의미합니다.

서명

문자열 isEmpty() 메소드의 서명 또는 구문은 다음과 같습니다.

컴퓨터가 뭐야?
 public boolean isEmpty() 

보고

길이가 0이면 true, 그렇지 않으면 false입니다.

부터

1.6

내부 구현

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

Java 문자열 isEmpty() 메소드 예

파일 이름: StringIsEmptyExample.java

자바 문자를 int로
 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 문자열 isEmpty() 메소드 예 2

파일 이름: StringIsEmptyExample2.java

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

산출:

 String s1 is empty Javatpoint 

비어 있음 대. 널 문자열

이 튜토리얼의 앞부분에서 빈 문자열에 0개의 문자가 포함되어 있다는 점을 논의했습니다. 그러나 null 문자열의 경우에도 마찬가지입니다. 널 문자열은 값이 없는 문자열입니다.

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

isEmpty() 메소드는 널 문자열을 확인하는 데 적합하지 않습니다. 다음 예에서는 동일한 내용을 보여줍니다.

파일 이름: 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) 

여기서는 == 연산자를 사용하여 null 문자열을 확인할 수 있습니다.

파일 이름: 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() 메서드는 빈 문자열을 확인하는 데 매우 유용합니다. 다음 예를 고려하십시오.

캐시 지우기 npm

파일 이름: 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.