ApexSensei

Drills › Write Apex

Quick win: is the name blank?

Write Apex · Beginner · 5 min · Runs in your Salesforce Developer org · Free

Scenario. A form must refuse a Contact whose name is missing, even when someone typed only spaces.

Use String.isBlank to treat null, empty text and spaces the same way.

What you will learn

Three things look empty to a user: null, '' and ' '. String.isBlank returns true for all three, so you never have to check them one by one.

Boolean isMissingPhone(String phone) {
    return String.isBlank(phone);
}

Gotcha. value == '' misses null and spaces. value.length() == 0 throws on null.

The task

Write a method isBlankName(String name) that returns true when the name is missing, empty or only spaces.

Examples:

  • isBlankName('Ana') → false
  • isBlankName('') → true
  • isBlankName(null) → true

Requirements:

  1. Return the result of String.isBlank(name).
  2. Spaces only, like ' ', count as blank.
  3. Keep the method's name and its String input.

Starter code

Boolean isBlankName(String name) {
    return null;
}

System.debug('Ana: ' + isBlankName('Ana'));
System.debug('Empty: ' + isBlankName(''));

When it passes

`isBlankName` returns true for null, empty text and spaces, and false for a real name.

Try this drill free

You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.

‹ Quick win: join a full name · Integer division and Math.mod ›