Logo

Programming-Idioms

History of Idiom 110 > diff from v85 to v86

Edit summary for version 86 by programming-idioms.org:
[Java] strip() better than trim()

Version 85

2022-03-03, 23:35:18

Version 86

2022-03-15, 14:51:48

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.trim().isEmpty();
Code
boolean blank = s==null || s.strip().isEmpty();
Comments bubble
Use strip (Java 11) instead of trim for Unicode-awareness, or more simply use isBlank (Java 11)
Comments bubble
Java 11
Doc URL
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#trim()
Doc URL
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#trim()
Demo URL
https://replit.com/@ProgIdioms/CheerfulAzureEnvironment#Main.java
Demo URL
https://replit.com/@ProgIdioms/CheerfulAzureEnvironment#Main.java