diff --git a/pkgs/racket-doc/scribblings/reference/strings.scrbl b/pkgs/racket-doc/scribblings/reference/strings.scrbl index 6b16f7c236..eb991e8993 100644 --- a/pkgs/racket-doc/scribblings/reference/strings.scrbl +++ b/pkgs/racket-doc/scribblings/reference/strings.scrbl @@ -397,6 +397,13 @@ one between @racket[list] and @racket[list*]. ]} +@defproc[(string-endswith? [str string?] + [suffix string?]) + boolean?]{ + +Returns @racket[#t] if the second argument is a suffix of the first.} + + @defproc[(string-join [strs (listof string?)] [sep string? " "] [#:before-first before-first string? ""] [#:before-last before-last string? sep] @@ -459,6 +466,13 @@ replaced if @racket[all?] is @racket[#f]. ]} +@defproc[(string-startswith? [str string?] + [prefix string?]) + boolean?]{ + +Returns @racket[#t] if the second argument is a prefix of the first.} + + @defproc[(string-split [str string?] [sep (or/c string? regexp?) #px"\\s+"] [#:trim? trim? any/c #t] diff --git a/pkgs/racket-test-core/tests/racket/string.rktl b/pkgs/racket-test-core/tests/racket/string.rktl index cf0a959119..e0eec0caaa 100644 --- a/pkgs/racket-test-core/tests/racket/string.rktl +++ b/pkgs/racket-test-core/tests/racket/string.rktl @@ -490,4 +490,23 @@ (test "_1_ !!!" string-replace "_1_ _2_" str "!!!") ;verify that the new str is used ) +;; ---------- string-starts/endswith ---------- +(let () + (test #t string-startswith? "racket" "") + (test #t string-startswith? "racket" "r") + (test #t string-startswith? "racket" "rack") + (test #t string-startswith? "racket" "racket") + (test #t string-endswith? "racket" "") + (test #t string-endswith? "racket" "t") + (test #t string-endswith? "racket" "cket") + (test #t string-endswith? "racket" "racket") + (test #f string-startswith? "" "racket") + (test #f string-startswith? "racket" "R") + (test #f string-startswith? "racket" "rak") + (test #f string-startswith? "racket" "racket2") + (test #f string-endswith? "" "racket") + (test #f string-endswith? "racket" "T") + (test #f string-endswith? "racket" "r") + (test #f string-endswith? "racket" "kat")) + (report-errs) diff --git a/racket/collects/racket/string.rkt b/racket/collects/racket/string.rkt index 6e8ad8d18b..3d0db26729 100644 --- a/racket/collects/racket/string.rkt +++ b/racket/collects/racket/string.rkt @@ -6,7 +6,9 @@ string-normalize-spaces string-split string-replace - non-empty-string?) + non-empty-string? + string-startswith? + string-endswith?) (define string-append* (case-lambda [(strs) (apply string-append strs)] ; optimize common cases @@ -138,3 +140,19 @@ (define (non-empty-string? x) (and (string? x) (not (zero? (string-length x))))) + +(define (string-startswith? str prefix) + (and + (<= (string-length prefix) (string-length str)) + (for/and ([c1 (in-string str)] + [c2 (in-string prefix)]) + (char=? c1 c2)))) + +(define (string-endswith? str suffix) + ;; Skip all but the last `suffix` characters of `str` + (define offset (- (string-length str) (string-length suffix))) + (and + (not (negative? offset)) + (for/and ([c1 (in-string str offset)] + [c2 (in-string suffix)]) + (char=? c1 c2))))