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

Idiom #168 Trim suffix

Create string t consisting of string s with its suffix w removed (if s ends with w).

t = s.sub(/#{w}\z/, "")
import "strings"
t := strings.TrimSuffix(s, w)
uses StrUtils;
if AnsiEndsStr(w, s) then
  t := copy(s, 1, length(s) - length(w))
else
  t :=s;

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org

Related idioms