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;

I use the isBlank(String) method on the string class in order to check whether a string is either null, white space or empty.


The documentation says:

Returns true if the specified String is white space, empty (''), or null; otherwise, returns false.

| improve this answer | |
  • 5
    It's worth noting that this is effectively a combination of 1 and 2 in @RobertSosemann's post as it catches both null and ''. – Alex Tennant May 20 '15 at 7:58
  • 1
    and it would even catch strings with white space only like ' '. – Andree Wille May 20 '15 at 9:24
  • 7
    Note: If checking from a query, pre- and post-whitespace is automatically trimmed, and if the result would be an empty space, the field is instead null. That means that you virtually never need to check for '' (empty string). – sfdcfox May 20 '15 at 15:53
  • Thanks. It's like the difference between String.IsNullOrEmpty and String.IsNullOrWhiteSpace in C#/.NET. – wp78de Jan 4 '18 at 18:35
  • There are also isNotEmpty() and isNotBlank(), please check out the official docs here: developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – Bahman.A May 21 at 18:42

The difference is slight between isEmpty() and isBlank()

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.


In case, your check is for a null string or a string which does not contain any value(''), but in your usecase 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

Hope this helps..

| improve this answer | |

There's a method isEmpty(String), which returns true if string is null or empty. Unlike isBlank(String), returns false if string is white spaces.

| improve this answer | |

You can use this:

    Boolean isBlank = true;

The above is same as:

if(record.txt_Field__c == null || record.txt_Field__c == '' || record.txt_Field__c.trim.length() == 0) {
    Boolean isBlank = true;
| improve this answer | |
  • 2
    Why would you use this over the built in String.isBlank? – ca_peterson May 22 '15 at 3:37
  • 1
    its better to use String.isBlank since it checks for all three conditions – Walker May 22 '15 at 3:41

There are also isNotEmpty() and isNotBlank(), check out the official documentation for String class in Apex

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.