Logo

Programming-Idioms

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

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
t = s.Substring(s.Length - 5)
t = Right(s, 5)
(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

New implementation...