How to check in string isblank Apex?

499    Asked by ClaudineTippins in Salesforce , Asked on May 11, 2023

I'm unsure what's the shortest and most robust way to check whether a Text field is blank / empty?

/*1*/ Boolean isBlank = record.txt_Field__c == ''; /*2*/ Boolean isBlank = record.txt_Field__c == null; /*3*/ Boolean isBlank = record.txt_Field__c.trim() == ''; /*4*/ Boolean isBlank = record.txt_Field__c.size() == 0;
Answered by Dipesh Bhardwaj
The difference is slight between isEmpty() and string isBlank()apex
isBlank(inputString): Returns true if the specified String is white space, empty (''), or null; otherwise, returns false.
isEmpty(inputString) : Returns true if the specified String is empty ('') or null; otherwise, returns false.
So the isEmpty() function is a subset of isBlank() function.

Usage

In case, your check is for a null string or a string which does not contain any value(''), but in your use case a String having a space is valid then use isEmpty().
If you want to check if your string is null or have no value or have only blank spaces you should use isBlank
To know more about the String class you can follow the following link : Documentation

Your Answer

Interviews

Parent Categories