How to check whether a String is null or empty or blank using Apex in Salesforce?

How to check whether a String is null or empty or blank using Apex in Salesforce?

To check whether a String is null or empty or blank using Apex in Salesforce, we can use the following methods

  isBlank
Returns true if the specified
String is white space, empty (”), or null; otherwise, returns false.
  isEmpty
Returns true if the specified
String is empty (”) or null; otherwise, returns false.
  isNotBlank
Returns true if the specified
String is not whitespace, not empty (”), and not null; otherwise, returns false.
  isNotEmpty
Returns true if the specified
String is not empty (”) and not null; otherwise, returns false.

Sample Code: 

        String var = ‘abc’;
        if(String.isNotBlank(var)) {
            //The string is not empty or blank or null
        }

        else {
            //The string is empty or blank or null
        }


Cheers!!!

Leave a Reply