Logo

Programming-Idioms

History of Idiom 110 > diff from v103 to v104

Edit summary for version 104 by reilas:
[Java] Edited the explanation.

Version 103

2023-01-25, 12:39:59

Version 104

2023-01-26, 02:36:59

Idiom #110 Check if string is blank

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

Idiom #110 Check if string is blank

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

Code
boolean blank = s == null || s.equals("") || s.matches("\\s+");
Code
boolean blank = s == null || s.equals("") || s.matches("\\s+");
Comments bubble
The regular expression token \s will match a space and any of the following escape-sequences: \r \n \t \f \v.

The + will match one or more \s.
Comments bubble
The regular expression token \s will match a space and any of the following escape-sequences: \r \n \t \f \v.

The + will match one or more \s.

The double \ is done to prevent the Java compiler from reading \s as an escape-sequence.