String.trimPrefix
func String.trimPrefix(s: string, prefix: string): stringString.trimPrefix returns s with prefix removed once from its start, or s unchanged when it does not start with prefix.
Details
prefixis a string, not a set of characters. It is matched code point by code point, with no case folding or normalization, and removed once, not repeatedly.- An empty
prefixreturnss.
Basic examples
Remove a leading v from a version tag:
String.trimPrefix("v1.4.2", "v") // "1.4.2"A string that does not start with the prefix comes back unchanged:
String.trimPrefix("1.4.2", "v") // "1.4.2"Call it through . on any string:
"https://ant-lang.org".trimPrefix("https://") // "ant-lang.org"Scope
The prefix is removed once, even when it repeats:
"vvx".trimPrefix("v") // "vx"
"////a".trimPrefix("//") // "//a"An empty prefix changes nothing, and nothing is removed from an empty string:
"abc".trimPrefix("") // "abc"
"".trimPrefix("") // ""
"".trimPrefix("a") // ""The whole string as the prefix leaves the empty string. A prefix longer than s matches nothing:
"abc".trimPrefix("abc") // ""
"ab".trimPrefix("abc") // "ab"The prefix must be at the very start. A match later in the string does not count:
"a-b-".trimPrefix("b-") // "a-b-"Non-ASCII text works the same way, by code points:
"🐜🐝🐞".trimPrefix("🐜") // "🐝🐞"
"über-cool".trimPrefix("über-") // "cool"In a pipeline, the string is the first argument. Named arguments use s: and prefix::
"--verbose" | String.trimPrefix("--") // "verbose"
String.trimPrefix(s: "#heading", prefix: "#") // "heading"Applications
Read the value of a --name=value flag:
func flag(arg: string, name: string): string | null {
let p = "--${name}="
if arg.startsWith(p) { arg.trimPrefix(p) } else { null }
}
println(flag("--port=8080", "port"))
println(flag("--host=x", "port"))8080
nullTake the token out of an Authorization header:
let header = "Bearer abc.def.ghi"
header.trimPrefix("Bearer ") // "abc.def.ghi"Turn Markdown headings into plain titles, leaving other lines alone:
let text = "# Intro\nSome text.\n# Usage"
for line in text.lines() {
println(line.trimPrefix("# "))
}Intro
Some text.
UsageStrip a common directory from a list of paths:
let files = ["src/main.ant", "src/lib/geo.ant", "README.md"]
files.map({ x.trimPrefix("src/") }) // ["main.ant", "lib/geo.ant", "README.md"]Properties and relations
When s starts with prefix, putting the prefix back gives s. When it does not, the result is s:
for s in ["v1.0", "1.0", "v", ""] {
let t = s.trimPrefix("v")
if s.startsWith("v") {
assert("v" + t == s)
} else {
assert(t == s)
}
}When the prefix is present, the result is the slice of s behind it:
let s = "prefix-rest"
let p = "prefix-"
assert(s.startsWith(p))
assert(s.trimPrefix(p) == s.slice(p.length(), s.length()))The result differs from s exactly when s starts with a prefix that is not empty:
for s in ["ant", "hill", ""] {
assert((s.trimPrefix("an") != s) == s.startsWith("an"))
}String.trimSuffix does the same at the other end:
let s = "<b>"
s.trimPrefix("<").trimSuffix(">") // "b"Possible issues
The prefix is a string, not a set of characters to strip. "xy" matches only x followed by y:
"xxyz".trimPrefix("xy") // "xxyz"
"xyz".trimPrefix("xy") // "z"It removes one copy. To remove every leading copy, loop with String.startsWith:
var s = "0007"
while s.startsWith("0") {
s = s.trimPrefix("0")
}
println(s)7The match is case-sensitive. Lowering s first makes the prefix match, but it lowers the rest of s too:
"Http://Ant-Lang.org".trimPrefix("http://") // "Http://Ant-Lang.org"
"Http://Ant-Lang.org".toLower().trimPrefix("http://") // "ant-lang.org"To keep the case of the rest, compare only the start of s with String.equalFold:
let s = "Http://Ant-Lang.org"
let p = "http://"
if s.length() >= p.length() && String.equalFold(s.slice(0, p.length()), p) {
s.slice(p.length(), s.length())
} else {
s
} // "Ant-Lang.org"The match counts code points, not what a reader sees. Removing e from an e with a combining accent leaves the accent alone:
"e\u{301}tude".trimPrefix("e").startsWith("\u{301}") // trueTo remove white space, use String.trimStart, which takes no prefix:
" \tindent".trimStart() // "indent"Testing the result with startsWith does not tell whether a prefix was removed: "vv1" gives "v1", which still starts with v. Test s with String.startsWith first, or compare the result with s:
let s = "vv1"
s.trimPrefix("v").startsWith("v") // true
s.trimPrefix("v") != s // trueSee also
String.trimSuffix · String.startsWith · String.trimStart · String.trim · String.slice · String.cut · String.replaceFirst
