Quick win: is the name blank?
Apex basics · 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(x) returns true for all three, so you never have to check them one by one.
String phone = ' ';
Boolean missing = String.isBlank(phone);
missing is true. Your method should return that same call for the name it was given.
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')→falseisBlankName('')→trueisBlankName(null)→true
Requirements:
- Return the result of
String.isBlank(name). - Spaces only, like
' ', count as blank. - 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.
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 ›