Programming-Idioms

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

Idiom #150 Remove trailing slash

Remove last character from string p, if this character is a slash /.

p = p.chomp("/")
p.chomp!("/")
 if('/' == s.back())
  s.pop_back();
p = p.TrimEnd('/');
import std.string;
p=p.chomp("/");
import std.stdio : writeln;
import std.algorithm.mutation : stripRight;
auto p = "no_more_slashes///".stripRight!(a => a == '/');
writeln(p); // no_more_slashes
program x
character(len=:),allocatable :: string
integer :: ii
string='my string/'
ii=len(string)
string=trim(merge(string(:ii-1)//' ',string,string(ii:ii).eq.'/'))
write(*,*)string
end program x
import "strings"
p = strings.TrimSuffix(p, "/")
slashscrape l = if last l == '/' then init l else l
const slashscrape = p => (
  p.slice (-1) === '/' ?
    p.slice (0, -1) :
    p
)
if (p.endsWith("/")) {
    p = p.substring(0, p.length() - 1);
}
p = p.replaceAll("/$", "");
$p = trim($p, '/');
var
  Len: Integer;
begin
  Len := Length(p);
  if (Len > 0) and (p[Len] = '/') then Delete(P, Len, 1);
end;
SysUtils;
begin
  AllowDirectorySeparators := AllowDirectorySeparators + ['/'];
  p := ExcludeTrailingPathDelimiter(p);
end;
$p =~ s{/$}{};
p = p.rstrip("/")
if p.ends_with('/') { p.pop(); }
p.stripSuffix("/")

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

Idiom created by

programming-idioms.org

Related idioms