Logo

Programming-Idioms

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

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!

import "fmt"
import "os"
import "strings"
sep := fmt.Sprintf("%c", os.PathSeparator)
p = strings.TrimSuffix(p, sep)

os.PathSeparator is a rune, it must be converted to string.
import "fmt"
import "path/filepath"
import "strings"
sep := fmt.Sprintf("%c", filepath.Separator)
p = strings.TrimSuffix(p, sep)

filepath.Separator is a rune, it must be converted to string.
using System.IO;
p.TrimEnd(Path.DirectorySeparatorChar);

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