Logo

Programming-Idioms

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 the empty string!

using System.IO;
p.TrimEnd(Path.DirectorySeparatorChar);
import std.string,
import std.path;
p = p.chomp(dirSeparator);
import 'package:path/path.dart' as path;
if (p.endsWith(path.separator)) p = p.substring(0, p.length - 1);
import "fmt"
import "path/filepath"
import "strings"
sep := fmt.Sprintf("%c", filepath.Separator)
p = strings.TrimSuffix(p, sep)
import "fmt"
import "os"
import "strings"
sep := fmt.Sprintf("%c", os.PathSeparator)
p = strings.TrimSuffix(p, sep)
import Data.List (dropWhileEnd)
import System.FilePath (pathSeparator)
p' = dropWhileEnd (== pathSeparator) p
import * as path from 'path'
p = p.endsWith(path.sep) ? p.slice(0, -path.sep.length) : p
import static java.io.File.separatorChar;
int n = p.length() - 1;
if (n != 0 && p.charAt(n) == separatorChar)
    p = p.substring(0, n);
if string.sub(p, -1, -1) == "/" then
	p=string.sub(p, 1, -2)
end
uses LazFileUtils;
begin
  p := ChompPathDelim(p);
end.
uses SysUtils;
begin
  p := ExcludeTrailingPathDelimiter(p);
end.
use File::Spec qw();
chomp $p, File::Spec->catdir('');
import os
if p.endswith(os.sep):
    p = p[:-1]
p.chomp!("/")
p = p.strip_suffix(std::path::is_separator).unwrap_or(p);
let p = if ::std::path::is_separator(p.chars().last().unwrap()) {
    &p[0..p.len()-1]
} else {
    p
};

New implementation...
< >
programming-idioms.org