TIL: String.isBlank() vs isEmpty()
" ".isEmpty() returns false, but " ".isBlank() returns true — whitespace counts as blank, not empty.
1" ".isEmpty(); // false — length is 1
2" ".isBlank(); // true — whitespace only (Java 11+)
3"abc".isBlank(); // false
I caught myself writing s.trim().isEmpty() again today. String.isBlank() exists since Java 11 and covers exactly this case — plus it is Unicode-aware, so " \u00A0 ".isBlank() also works where trim() misses non-breaking spaces.
More posts
- Fastest Way To Insert the Data in MS SQL – Part 4 – Turning on the `useBulkCopyForBatchInsert` ParameterMay 8, 2024
- Fastest Way To Insert the Data in MS SQL – Part 3 – Multi-value insert vs Bulk ApiFeb 11, 2024
- Fastest Way To Insert the Data in MS SQL – Part 2 – Hibernate vs Prepared Statement vs Jdbc Template vs Bulk ApiOct 21, 2023