add string-startswith? and string-endswith?
This commit is contained in:
parent
bcfd19c902
commit
22cda63200
|
@ -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]
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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))))
|
||||
|
|
Loading…
Reference in New Issue
Block a user