hasPrefix
lib.hasPrefix
Docs pulled from | This Revision | 4 days 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
Implementation
The following is the current implementation of this function.
hasPrefix =
pref:
let
getGivenPrefix = substring 0 (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: getGivenPrefix str == pref;