Logo

Programming-Idioms

History of Idiom 110 > diff from v64 to v65

Edit summary for version 65 by Anonymous:
[Scala] Added Option(s).forall as without it a null string will raise a NPE on .trim

Version 64

2021-08-15, 23:40:11

Version 65

2021-08-17, 11:21:18

Idiom #110 Check if string is blank

Set boolean blank to true if string s is empty, or null, or contains only whitespace ; false otherwise.

Idiom #110 Check if string is blank

Set boolean blank to true if string s is empty, or null, or contains only whitespace ; false otherwise.

Code
val blank = s.trim().isEmpty()
Code
val blank = Option(s).forall(_.trim.isEmpty)
Comments bubble
Use strip (Java 11) instead of trim for Unicode-awareness, or more simply use isBlank (Java 11)
Comments bubble
Use strip (Java 11) instead of trim for Unicode-awareness, or more simply use isBlank (Java 11)

Option(s) ensures that null is handled.