query
On this page

removePrefix

lib.removePrefix

Docs pulled from | This Revision | 16 minutes ago


Return 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"
(lib.strings.removePrefix)

Noogle detected

Aliases

Implementation

The following is the current implementation of this function.

removePrefix =
    prefix: str:
    # Before 23.05, paths would be copied to the store before converting them
    # to strings and comparing. This was surprising and confusing.
    warnIf (isPath prefix)
      ''
        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.
            This behavior is deprecated and will throw an error in the future.''
      (
        let
          preLen = stringLength prefix;
        in
        if substring 0 preLen str == prefix then
          # -1 will take the string until the end
          substring preLen (-1) str
        else
          str
      );