String.endsWith
func String.endsWith(s: string, t: string): boolString.endsWith reports whether s ends with t.
Details
- Compares code points exactly: it does not ignore case and does not normalize, so the two spellings of "é" differ.
- The empty string is a suffix of every string, so
String.endsWith(s, "")is always true.
Basic examples
String.endsWith("main.ant", ".ant") // true
String.endsWith("main.ant", ".go") // falseCall it through . on any string:
let name = "report.tar.gz"
name.endsWith(".gz") // true
name.endsWith(".tar") // false
name.endsWith("tar.gz") // trueScope
Every string ends with the empty string, and the empty string ends with nothing else:
"ant".endsWith("") // true
"".endsWith("") // true
"".endsWith("t") // falseA suffix longer than the string never matches, even when the string is inside it:
"ant".endsWith("giant") // false
"giant".endsWith("ant") // trueText in any script compares by code points:
"Ωμέγα".endsWith("γα") // true
"東京都".endsWith("都") // true
"naïve".endsWith("ve") // trueIn a pipeline, the string is the first argument:
"index.html" | String.endsWith(".html") // trueApplications
Keep the files with one extension:
let files = ["main.ant", "notes.txt", "util.ant", "ant.png"]
files.filter({ x.endsWith(".ant") }) // ["main.ant", "util.ant"]Accept any of several suffixes with Array.some:
func isImage(name: string): bool {
[".png", ".jpg", ".gif"].some({ name.endsWith(x) })
}
isImage("ant.png") // true
isImage("ant.svg") // falseJoin shell lines that end with a backslash:
let lines = ["cc -o ant \\", " main.c \\", " util.c", "echo done"]
let commands: Array[string] = []
var pending = ""
for line in lines {
if line.endsWith("\\") {
pending += line.trimSuffix("\\")
} else {
commands.push(pending + line)
pending = ""
}
}
for c in commands {
println(c)
}cc -o ant main.c util.c
echo doneProperties and relations
String.endsWith agrees with slicing the last code points of s:
let s = "report.tar.gz"
for t in [".gz", ".tar", "", "report.tar.gz", "x.report.tar.gz"] {
let n = s.length()
let m = t.length()
assert(s.endsWith(t) == (m <= n && s.slice(n - m, n) == t))
}A string always ends with what is appended to it, and String.trimSuffix removes exactly that:
let s = "ant"
let t = "hill"
assert((s + t).endsWith(t))
assert((s + t).trimSuffix(t) == s)Reversing the code points of both strings turns endsWith into String.startsWith:
func reverse(s: string): string {
String.concat(s.chars().reverse())
}
for t in ["gz", "tar.gz", "tar", ""] {
let s = "report.tar.gz"
assert(s.endsWith(t) == reverse(s).startsWith(reverse(t)))
}On the UTF-8 bytes, Bytes.endsWith gives the same answer:
let s = "año"
for t in ["o", "ño", "ñ", ""] {
assert(s.endsWith(t) == Bytes.endsWith(s.toBytes(), t.toBytes()))
}Possible issues
The string comes first and the suffix second. Swapped, the call asks whether the suffix ends with the string:
String.endsWith(".ant", "main.ant") // false
String.endsWith("main.ant", ".ant") // trueCase matters, so lower both sides, or fold them with String.foldCase, to ignore it:
let name = "PHOTO.JPG"
name.endsWith(".jpg") // false
name.toLower().endsWith(".jpg") // true
name.foldCase().endsWith(".jpg") // trueThe precomposed "é" and "e" followed by a combining accent are different code points: the string "cafe\u{301}" ends with the accent, not with "é". Normalize both sides with String.normalize when text can arrive in either spelling:
let word = "cafe\u{301}"
word.endsWith("\u{301}") // true
word.endsWith("é") // false
word.normalize("NFC").endsWith("é".normalize("NFC")) // trueString.endsWith works on code points, not on what a reader sees as one character. A family emoji is three people joined by U+200D, so it ends with the last of them:
"👨👩👧".endsWith("👧") // trueString.graphemes shows the one character a reader sees:
"👨👩👧".graphemes().length() // 1There is no character type and no 'c' literal; a character is a string of one code point:
println(String.endsWith("abc", 'c')) // error: unexpected character '\''A nullable suffix is a compile error. Narrow it first:
let ext: string | null = null
println(String.endsWith("main.ant", ext))
// error: cannot use string | null where string is expectedString.endsWith takes one suffix, not an array of them; use Array.some, as isImage above does:
println(String.endsWith("ant.png", [".png", ".jpg"]))
// error: cannot use Array[string] where string is expectedSee also
String.startsWith · String.trimSuffix · String.contains · String.lastIndexOf · String.slice · Bytes.endsWith · Path.ext
