Logo

Programming-Idioms

  • PHP
  • Lisp

Idiom #97 Check string suffix

Set boolean b to true if string s ends with string suffix, false otherwise.

Are the last characters of s equal to this suffix?

(defun check-suffix (str suf )
  (if (> (length suf) (length str )) nil (equalp (subseq str (- (length str) (length suf)) (length str )) suf )))

(setf b (check-suffix s suffix))
$b = (bool)preg_match("/{$suffix}$/", $s);

Uses regular expression to check end of s for suffix
(require '[clojure.string :as str])
(let [b (str/ends-with? s suffix)]
   ; ...  
 )

New implementation...
< >
programming-idioms.org