In this article I’ll demonstrate how to check a String whether it is null or empty or blank using Apex.
we can use the following methods to check a whether String is null or empty or blank:
IsBlank – It Returns true if the specified String is white space, empty (”) or null, otherwise it returns false.
IsNotBlank – It Returns true if the specified String is not white space, not empty (”) and not null, otherwise it returns false.
IsEmpty – It Returns true if the specified String is empty (”) or null, otherwise it returns false.
IsNotEmpty – Returns true if the specified String is not empty (”) and not null, otherwise it returns false.
Sample Code:
public with sharing class CheckString { public String CheckIt {get;set;} public CheckString() { CheckIt = 'Biswajeet'; } //Check IsBlank public Boolean CheckisBlank() { if(String.isBlank(CheckIt)) { //String is not white space or not empty ('') or not null return false; } else { //String is white space or empty ('') or null return true; } } //Check IsNotBlank public Boolean CheckIsNotBlank() { if(String.isNotBlank(CheckIt)) { //String is not whitespace and not empty ('') and not null return true; } else { //String is whitespace or empty ('') or null return false; } } //Check IsEmpty public Boolean CheckIsEmpty() { if(String.isEmpty(CheckIt)) { //String is not empty ('') or not null return false; } else { //String is empty ('') or null return true; } } //Check IsNotEmpty public Boolean CheckisNotEmpty() { if(String.isNotEmpty(CheckIt)) { //String is not empty ('') and not null return true; } else { //String is empty ('') or blank or null return false; } } }