Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Clojure

Idiom #47 Extract string suffix

Create string t consisting in the 5 last characters of string s.
Make sure that multibyte characters are properly handled.

The end of the string s
(def t 
  (when (string? s)
    (let [from (-> s (count) (- 5) (max 0))]
      (subs s from))))

when with string? take care of nil and other types.
max takes care of strings shorter than 5.
-> (arguably) for readability.
subs uses java.lang.String/substring
(let [t (clojure.string/join (take-last 5 s))])
IDENTIFICATION DIVISION.
PROGRAM-ID. suffix.
PROCEDURE DIVISION.
    MOVE FUNCTION LENGTH(s) TO len
    COMPUTE pos = (len - 5) + 1
    MOVE    s(pos:) TO t    	 	
STOP RUN.

New implementation...