String.isEmpty
func String.isEmpty(s: string): boolString.isEmpty reports whether s is "".
Details
- A string of white space is not empty. Use
s.trim().isEmpty()to treat it as empty.
Basic examples
String.isEmpty("") // true
String.isEmpty("ant") // falseCall it through . on any string:
let name = "Ann"
name.isEmpty() // falseScope
White space is text. Only "" is empty:
" ".isEmpty() // false
"\n".isEmpty() // falseA code point that shows nothing, such as a zero-width space or a lone combining accent, still counts:
"\u{200B}".isEmpty() // false
"\u{301}".isEmpty() // falseIn a pipeline, the string is the first argument:
"" | String.isEmpty() // trueAs a function value, it maps an array of strings to an array of bools:
Array.map(["", "a", ""], String.isEmpty) // [true, false, true]For a string that may be null, ?. yields null instead of calling:
let nickname: string | null = null
nickname?.isEmpty() // null
nickname?.isEmpty() ?? true // trueApplications
Test what String.slice or String.trim returns:
"abc".slice(1, 1).isEmpty() // true
" \t\n".trim().isEmpty() // trueDrop the empty fields that a doubled or trailing separator leaves:
"a,,b,".split(",") // ["a", "", "b", ""]
"a,,b,".split(",").filter({ !x.isEmpty() }) // ["a", "b"]Fall back to a default when a name is blank:
func greet(name: string): string {
let trimmed = name.trim()
if trimmed.isEmpty() {
"Hello, stranger"
} else {
"Hello, ${trimmed}"
}
}
println(greet("Ann"))
println(greet(" "))Hello, Ann
Hello, strangerReject empty input with a Result:
type EmptyInput = {}
func parseName(s: string): Result[string, EmptyInput] {
if s.isEmpty() {
return Result.err(EmptyInput{})
}
Result.ok(s)
}
println(parseName("Ann"))
println(parseName(""))Ok{ value: "Ann" }
Err{ error: EmptyInput{} }Properties and relations
String.isEmpty agrees with len, String.length, == against "", and String.chars:
for s in ["", "a", " ", "日本", "👨👩👧"] {
assert(s.isEmpty() == (len(s) == 0))
assert(s.isEmpty() == (s.length() == 0))
assert(s.isEmpty() == (s == ""))
assert(s.isEmpty() == s.chars().isEmpty())
}The concatenation of two strings is empty exactly when both are:
for a in ["", "x"] {
for b in ["", "y"] {
assert((a + b).isEmpty() == (a.isEmpty() && b.isEmpty()))
}
}Possible issues
A string is not a condition. Write !s.isEmpty() instead:
let s = ""
if s { // error: cannot use string where bool is expected
println("has text")
}let s = ""
if !s.isEmpty() {
println("has text")
}A blank string is not empty. Trim it first to treat white space as nothing:
let input = " "
input.isEmpty() // false
input.trim().isEmpty() // trueString.isSpace does not treat "" as blank either:
"".isSpace() // false
" ".isSpace() // truetrim().isEmpty() covers both the empty and the blank string.
A nullable string, such as the result of Map indexing, is not a string, so passing it is a compile error:
let user = Map{ name: "" }
println(String.isEmpty(user["name"])) // error: cannot use string | null where string is expectedNarrow it first:
let user = Map{ name: "" }
let v = user["name"]
if v != null {
println(v.isEmpty())
}trueOr give a default with ??:
let user = Map{ name: "" }
(user["name"] ?? "").isEmpty() // trueA named primitive over string is not a string. Unwrap it with string(...):
type Name = string
let n = Name("")
println(String.isEmpty(n)) // error: cannot use Name where string is expectedtype Name = string
let n = Name("")
string(n).isEmpty() // trueSee also
String.length · String.trim · String.isSpace · String.chars · Array.isEmpty · Map.isEmpty · Set.isEmpty
