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).

using System;
string t = s.TrimEnd(w);
import std.string;
string t = s.chomp(w);
  i = index(s,w,back=.true.) - 1
  if (i == len(s) - len(w) ) then
     allocate (t, source=s(:i))
     allocate (t, source=s)
  end if
import "strings"
t := strings.TrimSuffix(s, w)
local t = s:gsub(w.."$", "")
uses StrUtils;
if AnsiEndsStr(w, s) then
  t := copy(s, 1, length(s) - length(w))
  t :=s;
if (length $s == rindex($s, $w) + length $w) {
    my $t = substr $s, 0, rindex $s, $w;
t = s.rsplit(w, 1)[0]
t = s.rstrip(w)
t = s.delete_suffix(w)
t = s.sub(/#{w}\z/, "")
let t = s.strip_suffix(w).unwrap_or(s);
let t = s.trim_end_matches(w);

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

Idiom created by

Related idioms