query
On this page

hasPrefix

lib.strings.hasPrefix

Docs pulled from | This Revision | 27 minutes ago


Nixpkgs manual

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:
    let
      lenPrefix = stringLength pref;
    in
    if isPath pref 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.hasPrefix: The first argument (${toString pref}) is a path value, but only strings are supported.
            You might want to use `lib.path.hasPrefix` instead, which correctly supports paths.''
    else
      str: substring 0 lenPrefix str == pref;