mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-03 19:58:18 +00:00
Add Str.replaceFirst
builtin
This commit is contained in:
parent
15eb36e4ab
commit
c5b6aef21a
1 changed files with 12 additions and 0 deletions
|
@ -33,6 +33,7 @@ interface Str
|
|||
toU8,
|
||||
toI8,
|
||||
toScalars,
|
||||
replaceFirst,
|
||||
splitFirst,
|
||||
splitLast,
|
||||
walkUtf8WithIndex,
|
||||
|
@ -276,6 +277,17 @@ countUtf8Bytes : Str -> Nat
|
|||
## string slice that does not do bounds checking or utf-8 verification
|
||||
substringUnsafe : Str, Nat, Nat -> Str
|
||||
|
||||
## Returns the string with the first occurrence of a substring replaced with a replacement.
|
||||
## If the substring is not found, returns `Err NotFound`.
|
||||
##
|
||||
## Str.replaceFirst "foo/bar/baz" "/" "_" == Ok "foo_bar/baz"
|
||||
replaceFirst : Str, Str, Str -> Result Str [NotFound]*
|
||||
replaceFirst = \haystack, needle, flower ->
|
||||
when splitFirst 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