query
On this page

removePrefix

lib.strings.removePrefix

Docs pulled from | This Revision | 24 minutes ago


Nixpkgs manual

Returns a string without the specified prefix, if the prefix matches.

Inputs

prefix
Prefix to remove if it matches
str
Input string

Type

removePrefix :: String -> String -> String

Examples

lib.strings.removePrefix usage example

removePrefix "foo." "foo.bar.baz"
=> "bar.baz"
removePrefix "xxx" "foo.bar.baz"
=> "foo.bar.baz"

Noogle detected

Aliases

Implementation

The following is the current implementation of this function.

removePrefix =
    prefix:
    let
      preLen = stringLength prefix;
    in
    if isPath prefix then
      # Before 23.05, paths would be copied to the store before converting them
      # to strings and comparing. This was surprising and confusing.
      throw ''
        lib.strings.removePrefix: The first argument (${toString prefix}) is a path value, but only strings are supported.
            There is almost certainly a bug in the calling code, since this function never removes any prefix in such a case.
            This function also copies the path to the Nix store, which may not be what you want.''
    else
      str:
      if substring 0 preLen str == prefix then
        # -1 will take the string until the end
        substring preLen (-1) str
      else
        str;