5

For string in Apex code, I can see two functions to check if string has any value or not: isNotBlank(inputString) and isNotEmpty(inputString). I have following questions regarding two similar looking function:

  1. What is the difference between isNotBlank(inputString) and isNotEmpty(inputString)?
  2. Can these functions also detect if inputString is null?

While I used above function with empty string, both behaved similarly (i.e. returned false). For null value as well both function returned false. Just confused when to use which function out of isNotBlank and isNotEmpty.

1
  • 1
    This question shows very little research effort. In the future, I recommend taking a close look at the documentation first. Questions clearly answered by the documentation on whatever class you are asking about often receive down votes.
    – Adrian Larson
    Commented Jun 20, 2018 at 21:49

2 Answers 2

8

TL;DR - If you want to return false when the input is whitespace, use isNotBlank. If you want this value to return true instead, use isNotEmpty.


This question is plainly answered in the documentation:

isNotBlank(inputString)
Returns true if the specified String is not whitespace, not empty (''), and not null; otherwise, returns false.

...

Example

String alpha = 'Hello world!';
System.assert(String.isNotBlank(alpha));
String blank = '';
String nullString = null;
String whitespace = '  ';
System.assert(!String.isNotBlank(blank));
System.assert(!String.isNotBlank(nullString));
System.assert(!String.isNotBlank(whitespace));

isNotEmpty(inputString)
Returns true if the specified String is not empty ('') and not null; otherwise, returns false.

...

Example

String whitespace = '  ';
String alpha = 'Hello world!';
System.assert(String.isNotEmpty(whitespace));
System.assert(String.isNotEmpty(alpha));
String empty = '';
String nullString = null;
System.assert(!String.isNotEmpty(empty));
System.assert(!String.isNotEmpty(nullString));
2
  • 2
    According to your detail null would return false for both. Your asserts show that as well since your are negating the result. Did I miss something
    – Eric
    Commented Nov 28, 2018 at 6:47
  • 2
    I just tested both notEmpty and notBlank with a null value and it returns false on both of them. A bit confused by your TL;DR
    – Eric
    Commented Nov 28, 2018 at 18:44
2

From salesforce page https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_System_String_isNotBlank

isNotBlank(inputString) Returns true if the specified String is not whitespace, not empty (''), and not null; otherwise, returns false.

isNotEmpty(inputString) Returns true if the specified String is not empty ('') and not null; otherwise, returns false.

3
  • 1
    It's hard to see what this answer adds which is not contained in the answer posted several months ago.
    – Adrian Larson
    Commented Nov 28, 2018 at 5:08
  • 2
    In the above comment, the distinction is made on null but on salesforce page, you can see both methods return true for null. Commented Nov 28, 2018 at 6:07
  • 1
    Correct this answer adds little value, except it is easier to read, and references the documentation. New documentation is out now and it is much better I believe. The above comment, the salesforce page has never suggested a null returns true. developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/… Commented Jul 30, 2021 at 3:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .