mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-04 04:08:19 +00:00
Add Str.replaceLast
builtin
This commit is contained in:
parent
c5b6aef21a
commit
2b65659a11
1 changed files with 12 additions and 0 deletions
|
@ -34,6 +34,7 @@ interface Str
|
|||
toI8,
|
||||
toScalars,
|
||||
replaceFirst,
|
||||
replaceLast,
|
||||
splitFirst,
|
||||
splitLast,
|
||||
walkUtf8WithIndex,
|
||||
|
@ -288,6 +289,17 @@ replaceFirst = \haystack, needle, flower ->
|
|||
"\(before)\(flower)\(after)"
|
||||
err -> err
|
||||
|
||||
## Returns the string with the last occurrence of a substring replaced with a replacement.
|
||||
## If the substring is not found, returns `Err NotFound`.
|
||||
##
|
||||
## Str.replaceLast "foo/bar/baz" "/" "_" == Ok "foo/bar_baz"
|
||||
replaceLast : Str, Str, Str -> Result Str [NotFound]*
|
||||
replaceLast = \haystack, needle, flower ->
|
||||
when splitLast haystack needle is
|
||||
Ok { before, after } ->
|
||||
"\(before)\(flower)\(after)"
|
||||
err -> err
|
||||
|
||||
## Returns the string before the first occurrence of a delimiter, as well as the
|
||||
## rest of the string after that occurrence. If the delimiter is not found, returns `Err`.
|
||||
##
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue