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

Idiom #151 Remove string trailing path separator

Remove last character from string p, if this character is the file path separator of current platform.

Note that this also transforms unix root path "/" into empty string!

import "fmt"
import "os"
import "strings"
sep := fmt.Sprintf("%c", os.PathSeparator)
p = strings.TrimSuffix(p, sep)
import "fmt"
import "path/filepath"
import "strings"
sep := fmt.Sprintf("%c", filepath.Separator)
p = strings.TrimSuffix(p, sep)
import std.string,
import std.path;
p = p.chomp(dirSeparator);
SysUtils
begin
  p := ExcludeTrailingPathDelimiter(p);
end.
LazFileUtils
begin
  p := ChompPathDelim(p);
end.
import os
if p.endswith(os.sep):
    p = p[:-1]
p.chomp!("/")
let p = if ::std::path::is_separator(p.chars().last().unwrap()) {
    &p[0..p.len()-1]
} else {
    p
};

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

Idiom created by

programming-idioms.org

Related idioms