Salesforce/Development
String.isEmpty() & String.isBlank() & String.isNotEmpty() & String.isNotBlank() 차이점
어디다쏨
2022. 4. 18. 21:40
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