Home > Blockchain >  First String begins with second String in Racket language
First String begins with second String in Racket language

Time:09-23

I'm designing a function that takes two Strings and returns a Boolean indicating whether the first string begins with the second in Racket language.

This is what I have right now:

(define (string-starts-with? s1 s2)
  (cond
  [(string=? s1 s2) #true]
  [else #false]))

CodePudding user response:

There's a built-in procedure for that, you can use:

(define (string-starts-with? s1 s2)
  ; checks if s1 starts with s2
  (string-prefix? s1 s2))

(string-starts-with? "foobar" "foo")
=> #t
(string-starts-with? "foobar" "baz")
=> #f
  • Related