Skip to content

String.trimPrefix ​

ant
func String.trimPrefix(s: string, prefix: string): string

String.trimPrefix returns s with prefix removed once from its start, or s unchanged when it does not start with prefix.

Details ​

  • prefix is 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 prefix returns s.

Basic examples ​

Remove a leading v from a version tag:

ant
String.trimPrefix("v1.4.2", "v") // "1.4.2"

A string that does not start with the prefix comes back unchanged:

ant
String.trimPrefix("1.4.2", "v") // "1.4.2"

Call it through . on any string:

ant
"https://ant-lang.org".trimPrefix("https://") // "ant-lang.org"

Scope ​

The prefix is removed once, even when it repeats:

ant
"vvx".trimPrefix("v")    // "vx"
"////a".trimPrefix("//") // "//a"

An empty prefix changes nothing, and nothing is removed from an empty string:

ant
"abc".trimPrefix("") // "abc"
"".trimPrefix("")    // ""
"".trimPrefix("a")   // ""

The whole string as the prefix leaves the empty string. A prefix longer than s matches nothing:

ant
"abc".trimPrefix("abc") // ""
"ab".trimPrefix("abc")  // "ab"

The prefix must be at the very start. A match later in the string does not count:

ant
"a-b-".trimPrefix("b-") // "a-b-"

Non-ASCII text works the same way, by code points:

ant
"🐜🐝🐞".trimPrefix("🐜")       // "🐝🐞"
"über-cool".trimPrefix("über-") // "cool"

In a pipeline, the string is the first argument. Named arguments use s: and prefix::

ant
"--verbose" | String.trimPrefix("--")         // "verbose"
String.trimPrefix(s: "#heading", prefix: "#") // "heading"

Applications ​

Read the value of a --name=value flag:

ant
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"))
text
8080
null

Take the token out of an Authorization header:

ant
let header = "Bearer abc.def.ghi"
header.trimPrefix("Bearer ") // "abc.def.ghi"

Turn Markdown headings into plain titles, leaving other lines alone:

ant
let text = "# Intro\nSome text.\n# Usage"
for line in text.lines() {
  println(line.trimPrefix("# "))
}
text
Intro
Some text.
Usage

Strip a common directory from a list of paths:

ant
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:

ant
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:

ant
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:

ant
for s in ["ant", "hill", ""] {
  assert((s.trimPrefix("an") != s) == s.startsWith("an"))
}

String.trimSuffix does the same at the other end:

ant
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:

ant
"xxyz".trimPrefix("xy") // "xxyz"
"xyz".trimPrefix("xy")  // "z"

It removes one copy. To remove every leading copy, loop with String.startsWith:

ant
var s = "0007"
while s.startsWith("0") {
  s = s.trimPrefix("0")
}
println(s)
text
7

The match is case-sensitive. Lowering s first makes the prefix match, but it lowers the rest of s too:

ant
"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:

ant
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:

ant
"e\u{301}tude".trimPrefix("e").startsWith("\u{301}") // true

To remove white space, use String.trimStart, which takes no prefix:

ant
"  \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:

ant
let s = "vv1"
s.trimPrefix("v").startsWith("v") // true
s.trimPrefix("v") != s            // true

See also ​

String.trimSuffix · String.startsWith · String.trimStart · String.trim · String.slice · String.cut · String.replaceFirst