728x90
String.isEmpty() & String.isBlank() & String.isNotEmpty() & String.isNotBlank() 4가지의 차이점에 대해서 예제를 통해 알아 보겠습니다.
// 예제 1
String text = '';
System.debug('String.isEmpty(text) : ' + String.isEmpty(text));
System.debug('String.isBlank(text) : ' + String.isBlank(text));
System.debug('String.isNotEmpty(text) : ' + String.isNotEmpty(text));
System.debug('String.isNotBlank(text) : ' + String.isNotBlank(text));
// 결과 1
String.isEmpty(text) : true
String.isBlank(text) : true
String.isNotEmpty(text) : false
String.isNotBlank(text) : false
// 예제 2
String text2 = null;
System.debug('String.isEmpty(text2) : ' + String.isEmpty(text2));
System.debug('String.isBlank(text2) : ' + String.isBlank(text2));
System.debug('String.isNotEmpty(text2) : ' + String.isNotEmpty(text2));
System.debug('String.isNotBlank(text2) : ' + String.isNotBlank(text2));
// 결과 2
String.isEmpty(text2) : true
String.isBlank(text2) : true
String.isNotEmpty(text2) : false
String.isNotBlank(text2) : false
// 예제 3
String text3 = ' ';
System.debug('String.isEmpty(text3) : ' + String.isEmpty(text3));
System.debug('String.isBlank(text3) : ' + String.isBlank(text3));
System.debug('String.isNotEmpty(text3) : ' + String.isNotEmpty(text3));
System.debug('String.isNotBlank(text3) : ' + String.isNotBlank(text3));
// 결과 3
String.isEmpty(text3) : false
String.isBlank(text3) : true
String.isNotEmpty(text3) : true
String.isNotBlank(text3) : false
예제와 같이 '' 와 null 일 때는 같은 결과를 나타내지만 ' ' 일 때는 다른 결과를 나타냅니다.
따라서 ' ' (공백만 들어 있는 String 값)도 빈 값으로 판단하려면 isBlank나 isNotBlank를 사용해야 합니다.
728x90
'Salesforce > Development' 카테고리의 다른 글
Apex에서 종속 선택 목록 값 가져오기 (Get Dependent Picklist Values in Apex) (0) | 2022.04.20 |
---|---|
Safe Navigation Operator 안전 탐색 연산자(?.) (0) | 2022.04.19 |
세일즈포스 사용자 정의 알림 (Salesforce Custom Notification) 설정 및 구현 (0) | 2021.04.13 |
Salesforce Standard / Custom Object Mass Delete 기능 구현 (0) | 2021.03.15 |
Custom Label Parameter (1) | 2019.12.02 |