String.isSpace
func String.isSpace(s: string): boolString.isSpace reports whether every code point of s has the White_Space property. It is false on "".
Details
truewhen every code point ofshas the Unicode White_Space property;falseon"".- Tab, line feed, carriage return, the no-break space U+00A0, and the line separator U+2028 all count. The zero-width space U+200B and the byte order mark U+FEFF do not.
Basic examples
String.isSpace(" ") // true
String.isSpace("\t\n") // true
String.isSpace(" a ") // falseCall it through .:
" ".isSpace() // trueScope
The empty string has no code points to test, and gives false:
String.isSpace("") // falseThe control characters tab, line feed, vertical tab, form feed, carriage return, and next line U+0085 are white space:
String.isSpace("\t") // true
String.isSpace("\n") // true
String.isSpace("\u{B}") // true
String.isSpace("\u{C}") // true
String.isSpace("\r") // true
String.isSpace("\u{85}") // trueSpace separators of other widths count, and so do the line and paragraph separators:
String.isSpace("\u{A0}") // true
String.isSpace("\u{2009}") // true
String.isSpace("\u{3000}") // true
String.isSpace("\u{2028}") // true
String.isSpace("\u{2029}") // trueInvisible characters that are not white space do not: the zero-width space, the byte order mark, and the Mongolian vowel separator:
String.isSpace("\u{200B}") // false
String.isSpace("\u{FEFF}") // false
String.isSpace("\u{180E}") // falseTest one code point at a time with String.chars, here in a pipe:
"a b\tc" | String.chars() | Array.map({ x.isSpace() }) // [false, true, false, true, false]Pass it to a higher-order function as a function value:
["a", " ", "", "\n", "\u{A0}"].filter(String.isSpace) // [" ", "\n", "\u{a0}"]Applications
Drop the lines of a text that hold only white space:
let text = "first\n \n\t\nsecond"
text.lines().filter({ !x.isSpace() }) // ["first", "second"]Collapse every run of white space into one space:
func squeeze(s: string): string {
var out = ""
var inSpace = false
for c in s.chars() {
if c.isSpace() {
if !inSpace {
out += " "
}
inSpace = true
} else {
out += c
inSpace = false
}
}
out
}
squeeze("one two\t\n three") // "one two three"Report the lines of a text that end in white space:
let source = "let a = 1 \nlet b = 2\nlet c = 3\t"
var number = 0
for line in source.lines() {
number += 1
let last = line.chars().last()
if last != null && last.isSpace() {
println("line ${number}: trailing white space")
}
}line 1: trailing white space
line 3: trailing white spaceProperties and relations
String.isSpace(s) is s not empty and every code point white space:
for s in ["", " ", "\t\n", "a", " a ", "\u{A0}", "\u{200B}"] {
let all = !s.isEmpty() && s.chars().every({ x.isSpace() })
assert(String.isSpace(s) == all)
}On a nonempty string, String.isSpace agrees with String.trim and String.fields, which use the same white space:
for s in [" ", "\t\n", "a", " a ", "\u{A0}", "\u{3000}", "\u{200B}", "\u{85}"] {
assert(String.isSpace(s) == (String.trim(s) == ""))
assert(String.isSpace(s) == String.fields(s).isEmpty())
}Possible issues
String.isSpace is false on the empty string, so it does not answer whether a string is blank. An empty line passes a filter that drops white-space lines:
"a\n\n \nb".lines().filter({ !x.isSpace() }) // ["a", "", "b"]To test for blank, empty included, compare the trimmed string with "":
"a\n\n \nb".lines().filter({ x.trim() != "" }) // ["a", "b"]Every code point must be white space, so String.isSpace does not answer whether a string contains white space. Use Array.some for that:
String.isSpace("a b") // false
"a b".chars().some({ x.isSpace() }) // trueThe zero-width space and the byte order mark are not white space. String.isSpace rejects them, and String.trim leaves them in place:
String.isSpace("\u{FEFF}") // false
String.trim("\u{FEFF} x ") // "\u{feff} x"The no-break space is white space, though it exists to keep its neighbors together. String.fields splits at it:
"10\u{A0}km".fields() // ["10", "km"]See also
String.trim · String.fields · String.lines · String.isControl · String.isGraphic · String.isPrint · String.category · String.chars · Array.every · Array.some
