일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- list
- 백준
- 배열
- 프로그래머스
- 자료형
- 문자열
- Provider
- ContentProvider
- Dependency
- 리스트
- Kotlin
- 백준파이썬
- android
- 자바리스트정렬
- Java
- Python
- Hilt
- 오블완
- 파이썬
- 자바
- compose
- 자바set
- programmers
- jetpack
- composelifecycle
- nullpointerexception방지
- filternotnull()
- 파이썬문법
- disposableeffect
- 티스토리챌린지
Archives
- Today
- Total
study gomi
Java 문자열 빈 문자열인지 확인하기 본문
728x90
반응형
문자열의 길이를 확인
public class EmptyStringCheck {
public static void main(String[] args) {
String str = "";
if (str.length() == 0) {
System.out.println("The string is empty.");
} else {
System.out.println("The string is not empty.");
}
}
}
isEmpty() 메서드를 사용
- 가장 간단하고 직관적인 방법.
public class EmptyStringCheck {
public static void main(String[] args) {
String str = "";
if (str.isEmpty()) {
System.out.println("The string is empty.");
} else {
System.out.println("The string is not empty.");
}
}
}
isBlank() 메서드를 사용 (Java11 이상)
- 공백 문자열까지 확인할 때 유용
public class EmptyStringCheck {
public static void main(String[] args) {
String str = " ";
if (str.isBlank()) {
System.out.println("The string is empty or blank.");
} else {
System.out.println("The string is not empty or blank.");
}
}
}
trim().isEmpty()를 사용
- trim() 메서드를 사용하여 문자열의 앞뒤 공백을 제거한 후 isEmpty() 메서드를 사용.
- 이 방법은 isBlank() 메서드와 유사한 기능을 제공.
public class EmptyStringCheck {
public static void main(String[] args) {
String str = " ";
if (str.trim().isEmpty()) {
System.out.println("The string is empty or only contains whitespace.");
} else {
System.out.println("The string is not empty and has non-whitespace characters.");
}
}
}
정리
- 길이 확인 : str.length() == 0
- isEmpty() 메서드 : str.isEmpty()
- isBlank() 메서드 : str.isBlank() (Java 11 이상)
- trim().isEmpty() : str.trim().isEmpty()
728x90
반응형
'basic > java' 카테고리의 다른 글
Java 배열 초기화 / 배열 리터럴의 사용 방법 (0) | 2024.06.11 |
---|---|
Java List와 ArrayList 차이 (인터페이스와 클래스 / new 키워드) (0) | 2024.06.04 |
Java 배열(Array)과 리스트(List) 비교 / 차이 / 설명 (0) | 2024.06.04 |
Java 대소문자 변환 / StringBuilder 클래스 정리 (0) | 2024.05.28 |
Java 출력하기 정리 (숫자-정수/실수, 문자열, 여러 가지 타입 - 포맷팅) (0) | 2024.05.23 |