diff --git a/collects/racket/string.rkt b/collects/racket/string.rkt index b4a55bb3cd..61af77635b 100644 --- a/collects/racket/string.rkt +++ b/collects/racket/string.rkt @@ -5,8 +5,7 @@ string-trim string-normalize-spaces string-split - string-replace - string-replace*) + string-replace) (define string-append* (case-lambda [(strs) (apply string-append strs)] ; optimize common case @@ -94,22 +93,17 @@ space)) (define replace-cache (make-weak-hasheq)) -(define (replace-internal who str from to all?) - (unless (string? str) (raise-type-error who "string" str)) - (unless (string? to) (raise-type-error who "string" to)) +(define (string-replace str from to #:all? [all? #t]) + (unless (string? str) (raise-type-error 'string-replace "string" str)) + (unless (string? to) (raise-type-error 'string-replace "string" to)) (define from* (if (regexp? from) from (hash-ref! replace-cache from (λ() (if (string? from) (regexp (regexp-quote from)) - (raise-type-error who "string" from)))))) + (raise-type-error 'string-replace "string" from)))))) (define to* (regexp-replace-quote to)) (if all? (regexp-replace* from* str to*) (regexp-replace from* str to*))) - -(define (string-replace str from to) - (replace-internal 'string-replace str from to #f)) -(define (string-replace* str from to) - (replace-internal 'string-replace* str from to #t)) diff --git a/collects/scribblings/reference/strings.scrbl b/collects/scribblings/reference/strings.scrbl index 156e16c886..7b4ad65a29 100644 --- a/collects/scribblings/reference/strings.scrbl +++ b/collects/scribblings/reference/strings.scrbl @@ -472,5 +472,23 @@ You can specify @racket[space] for an alternate space replacement. Note that this is the same as @racket[(string-join (string-split str sep ....) space)]} +@defproc[(string-replace [str string?] + [from (or/c string? regexp?)] + [to string?] + [#:all all? any/c #t]) + string?]{ + +Returns a copy of @racket[str] where all occurrences of @racket[from] +are replaced with with @racket[to]. + +When @racket[from] is a string it is matched literally. The replacement +@racket[to] argument must be a string and is always inserted as-is. All +occurrences are replaced by default, pass @racket[#f] for @racket[all?] +to replace only the first match. + +@mz-examples[#:eval string-eval + (string-replace "foo bar baz" "bar" "blah") +]} + @close-eval[string-eval] diff --git a/collects/tests/racket/string.rktl b/collects/tests/racket/string.rktl index 7a47d29b8c..ab6bb40506 100644 --- a/collects/tests/racket/string.rktl +++ b/collects/tests/racket/string.rktl @@ -446,8 +446,8 @@ (test "" string-replace "" "|" "?") (test "foo" string-replace "foo" "|" "?") (test "foo\\bar" string-replace "foo\\bar" "\\" "\\") - (test "foo[bar]" string-replace "foo]bar]" "]" "[") - (test "foo[bar[" string-replace* "foo]bar]" "]" "[") + (test "foo[bar]" string-replace "foo]bar]" "]" "[" #:all? #f) + (test "foo[bar[" string-replace "foo]bar]" "]" "[") (test "foo\\1bar" string-replace "foo===bar" #rx"(=+)" "\\1") (test "foo\\1bar" string-replace "foo===bar" #px"={3}" "\\1"))