String.startsWith
func String.startsWith(s: string, t: string): boolString.startsWith reports whether s begins with t.
Details
- Every string starts with
"". - The match is exact, code point by code point as
==compares: there is no case folding or normalization, so the two spellings of "é" differ.
Basic examples
String.startsWith("https://ant-lang.org", "https://") // true
String.startsWith("https://ant-lang.org", "http://") // falseCall it through . on any string:
let flag = "--verbose"
flag.startsWith("--") // true
flag.startsWith("-") // true
flag.startsWith("-v") // falseScope
Every string starts with the empty string, and the empty string starts with nothing else:
String.startsWith("ant", "") // true
String.startsWith("", "") // true
String.startsWith("", "a") // falseA prefix longer than the string never matches, even when the string is inside it:
String.startsWith("ant", "anthill") // false
String.startsWith("anthill", "ant") // trueA string starts with itself:
String.startsWith("ant", "ant") // trueText in any script compares by code points:
String.startsWith("Ωμέγα", "Ωμ") // true
String.startsWith("東京都", "東京") // true
String.startsWith("ñandú", "ñ") // trueIn a pipeline, the string is the first argument:
"# title" | String.startsWith("#") // trueNaming the arguments makes clear which string is the prefix, in either order:
String.startsWith(s: "v1.2.0", t: "v") // true
String.startsWith(t: "v", s: "v1.2.0") // trueOn a string that can be null, use ?. and supply a default with ??:
let name: string | null = null
name?.startsWith("x") ?? false // falseApplications
Drop the comment lines of a config file:
let text = "# settings\nport=8080\n# debug=true\nhost=localhost"
text.lines().filter({ !x.trimStart().startsWith("#") }) // ["port=8080", "host=localhost"]Split command-line arguments into flags and operands:
let args = ["-v", "main.ant", "--out", "build", "util.ant"]
let flags = args.filter({ x.startsWith("-") })
let operands = args.filter({ !x.startsWith("-") })
println(flags)
println(operands)["-v", "--out"]
["main.ant", "build", "util.ant"]Pick a handler by the scheme of a URL:
func scheme(url: string): string {
if url.startsWith("https://") {
"secure web"
} else if url.startsWith("http://") {
"web"
} else if url.startsWith("file://") {
"local file"
} else {
"unknown"
}
}
println(scheme("https://ant-lang.org"))
println(scheme("http://example.com"))
println(scheme("file:///tmp/x"))
println(scheme("ftp://example.com"))secure web
web
local file
unknownProperties and relations
String.startsWith agrees with slicing the first code points of s:
let s = "anthill"
for t in ["ant", "anth", "", "anthill", "anthills", "hill"] {
let m = t.length()
assert(s.startsWith(t) == (m <= s.length() && s.slice(0, m) == t))
}String.startsWith holds exactly when String.indexOf finds t at index 0:
let s = "banana"
for t in ["ban", "an", "", "x"] {
assert(s.startsWith(t) == (s.indexOf(t) == 0))
}A string always starts with what is prepended to it, and String.trimPrefix removes exactly that:
let s = "hill"
let t = "ant"
assert((t + s).startsWith(t))
assert((t + s).trimPrefix(t) == s)Reversing the code points of both strings turns startsWith into String.endsWith:
func reverse(s: string): string {
String.concat(s.chars().reverse())
}
for t in ["re", "report", "port", ""] {
let s = "report.tar.gz"
assert(s.startsWith(t) == reverse(s).endsWith(reverse(t)))
}On the UTF-8 bytes, Bytes.startsWith gives the same answer:
let s = "año"
for t in ["a", "añ", "ñ", ""] {
assert(s.startsWith(t) == Bytes.startsWith(s.toBytes(), t.toBytes()))
}Possible issues
The string comes first and the prefix second. Swapped, the call asks whether the prefix begins with the string, which is false whenever the string is longer:
String.startsWith("http", "http://ant-lang.org") // false
String.startsWith("http://ant-lang.org", "http") // trueA short prefix also matches longer words. "http" matches an https URL too, so include the separator when it matters:
let url = "https://ant-lang.org"
url.startsWith("http") // true
url.startsWith("http://") // falseCase matters. Fold both sides with String.foldCase to compare without regard to case:
let header = "Content-Type: text/html"
header.startsWith("content-type") // false
header.foldCase().startsWith("Content-Type".foldCase()) // trueFolding changes more than case, so fold the prefix too:
"Straße".foldCase().startsWith("straß") // false
"Straße".foldCase().startsWith("STRAß".foldCase()) // trueLeading white space is part of the string. Trim it with String.trimStart first:
let line = " # note"
line.startsWith("#") // false
line.trimStart().startsWith("#") // trueThe precomposed "é" and "e" followed by a combining accent are different code points. Normalize both sides with String.normalize when text can arrive in either spelling:
let word = "e\u{301}cole"
word.startsWith("é") // false
word.normalize("NFC").startsWith("é".normalize("NFC")) // trueString.startsWith 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 starts with the first of them:
String.startsWith("👨👩👧", "👨") // trueThere is no character type and no 'c' literal; a character is a string of one code point:
println(String.startsWith("abc", 'a')) // error: unexpected character '\''A nullable prefix is a compile error. Narrow it first:
let prefix: string | null = null
println(String.startsWith("main.ant", prefix))
// error: cannot use string | null where string is expectedString.startsWith takes one prefix, not an array of them; use Array.some:
println(String.startsWith("https://x", ["http://", "https://"]))
// error: cannot use Array[string] where string is expected["http://", "https://"].some({ "https://x".startsWith(x) }) // trueSee also
String.endsWith · String.trimPrefix · String.contains · String.indexOf · String.cut · String.slice · Bytes.startsWith
