String.trimEnd
func String.trimEnd(s: string): stringString.trimEnd returns s without the trailing code points String.isSpace accepts.
Details
- Removes all white space at the end; white space at the start and in the middle stays.
- White space is Unicode white space, as
String.isSpaceaccepts, so the no-break space U+00A0 and the ideographic space U+3000 are removed too. - A string of only white space gives
"".
Basic examples
String.trimEnd("hello ") // "hello"
String.trimEnd(" hi ") // " hi"Call it through . on any string:
let line = "total: 42\r\n"
line.trimEnd() // "total: 42"Scope
The empty string, a string with nothing to remove, and a string of only white space:
"".trimEnd() // ""
"ant".trimEnd() // "ant"
" \t\n\r".trimEnd() // ""Space between words stays; only the end changes:
"a b ".trimEnd() // "a b"Every kind of white space at the end goes, the vertical tab, the form feed, the no-break space, and the ideographic space among them:
"x\u{B}\u{C}\u{A0}\u{3000}".trimEnd() // "x"Text in any script stays as it is, and so does a combining accent right before the white space: the five code points of "cafe\u{301}" remain:
"東京 ".trimEnd() // "東京"
"cafe\u{301} ".trimEnd().length() // 5In a pipeline, the string is the first argument:
"done\n" | String.trimEnd() // "done"The parameter is named s, so the call can name it:
String.trimEnd(s: "ant ") // "ant"Applications
Strip the trailing spaces from each line of a text, as an editor does on save:
let text = "func f() { \n return 1 \n}\t"
text
| String.lines()
| Array.map({ x.trimEnd() })
| String.join("\n") // "func f() {\n return 1\n}"Compare two versions of a text and ignore trailing white space, as diff -b does at line ends:
func clean(s: string): Array[string] {
s.lines().map({ x.trimEnd() })
}
func sameText(a: string, b: string): bool {
clean(a) == clean(b)
}
sameText("one \ntwo\n", "one\ntwo \n") // true
sameText("one\ntwo", "one\n two") // falseRight-align a column of values that came with padding on the right:
let cells = ["12 ", "3 ", "456 "]
cells.map({ x.trimEnd().padStart(5) }) // [" 12", " 3", " 456"]Properties and relations
String.trimEnd never adds anything: s starts with its result, and the part it removed is all white space:
for s in ["ant ", " ant", " ", "", "a b\t\n"] {
let t = s.trimEnd()
assert(s.startsWith(t))
let rest = s.slice(t.length(), s.length())
assert(rest == "" || rest.isSpace())
}Trimming twice is the same as trimming once:
let s = "x \t "
assert(s.trimEnd().trimEnd() == s.trimEnd())String.trimStart after String.trimEnd is String.trim, in either order:
for s in [" a b ", "\ta", "b\n", "", " "] {
assert(s.trimEnd().trimStart() == s.trim())
assert(s.trimStart().trimEnd() == s.trim())
}Possible issues
String.trimEnd leaves white space at the start. Use String.trim to remove both ends:
" ant ".trimEnd() // " ant"
" ant ".trim() // "ant"It removes white space only. To remove a given ending, such as a period or a /, use String.trimSuffix:
"end.".trimEnd() // "end."
"end.".trimSuffix(".") // "end"It removes every white space code point at the end, not one line break. A blank last line goes too, along with trailing spaces you may have meant to keep:
"a \n\n".trimEnd() // "a"The zero-width space U+200B and the byte order mark U+FEFF are not white space, so they stay:
"x\u{200B}".trimEnd().length() // 2
"x\u{FEFF}".trimEnd().length() // 2The function takes no set of characters to remove; a second argument is a compile error:
println(String.trimEnd("x--", "-")) // error: String.trimEnd takes 1 argument, got 2See also
String.trim · String.trimStart · String.trimSuffix · String.isSpace · String.fields · String.lines · String.padEnd
