query
On this page

hasPrefix

lib.strings.hasPrefix

Docs pulled from | This Revision | 14 minutes ago


Determine whether a string has given prefix.

Inputs

pref
Prefix to check for
str
Input string

Type

hasPrefix :: string -> string -> bool

Examples

lib.strings.hasPrefix usage example

hasPrefix "foo" "foobar"
=> true
hasPrefix "foo" "barfoo"
=> false

Noogle detected

Aliases

Implementation

The following is the current implementation of this function.

hasPrefix =
    pref:
    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 pref)
      ''
        lib.strings.hasPrefix: The first argument (${toString pref}) is a path value, but only strings are supported.
            There is almost certainly a bug in the calling code, since this function always returns `false` 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.
            You might want to use `lib.path.hasPrefix` instead, which correctly supports paths.''
      (substring 0 (stringLength pref) str == pref);