Check for empty text
An empty string has no characters. A space is still a character.
String.isEmpty
Simple example
Is a string with no characters empty?
Start''
Resulttrue
import Foundation
print("".isEmpty)
true
Advertisements
Everyday examples
A library card has no name entered. Should the form ask for one?
Start''
Resulttrue
import Foundation
print("".isEmpty)
true
A passenger typed a space in the seat field. Is the raw field empty?
Start' '
Resultfalse
import Foundation
print(" ".isEmpty)
false
The classroom screen says Ready. Is the message empty?
Start'Ready'
Resultfalse
import Foundation
print("Ready".isEmpty)
false