How to check in Apex if a Text field is blank

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

String.isBlank(record.txt_Field__c);

The documentation says:

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


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.

Usage

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..


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.