C# Path TrimEndingDirectorySeparator() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

In C#, Path.TrimEndingDirectorySeparator() removes one trailing separator from a path. "/data/reports/" becomes "/data/reports". A path with no trailing separator is returned unchanged.

It keeps a root as it is: "/" stays "/" and, on Windows, C:\ stays C:\. It is available in .NET Core 3.0 and newer (and .NET Standard 2.1), together with the matching question method Path.EndsInDirectorySeparator.

This is a static method of System.IO.Path. Call it as Path.TrimEndingDirectorySeparator(...); there is no new Path(). Like most Path methods, it only works with text: it never opens, reads, or creates a file, and it works the same whether or not the path exists.

Syntax

Pass the path. The result is a string, and no folder is touched.

C#
string trimmed = Path.TrimEndingDirectorySeparator("/data/reports/");
Console.WriteLine(trimmed);   // /data/reports

bool before = Path.EndsInDirectorySeparator("/data/reports/");   // true
bool after = Path.EndsInDirectorySeparator(trimmed);             // false
CallWhat it does
TrimEndingDirectorySeparator("/a/b/")Returns /a/b.
TrimEndingDirectorySeparator("/a/b")Nothing to trim. Returns /a/b.
TrimEndingDirectorySeparator("/")A root keeps its separator. Returns /.
TrimEndingDirectorySeparator("/a//")Only one separator is removed. Returns /a/.
EndsInDirectorySeparator(path)A separate method that answers true or false and changes nothing.

Both / and \ count as separators on Windows. On Linux and macOS only / does, so a trailing backslash is an ordinary character there and is not trimmed.

Library declarations
C#
public static string TrimEndingDirectorySeparator(string path);
public static ReadOnlySpan<char> TrimEndingDirectorySeparator(ReadOnlySpan<char> path);
public static bool EndsInDirectorySeparator(string path);

Parameters

ParameterDescription
pathThe path to trim. It is a string or a ReadOnlySpan<char>. An empty string comes back empty.

Return Value

Return ValueDescription
stringThe path without one trailing separator, or the same path if it had none or is a root. The span version returns a shorter view and does not copy.

C# Examples

Example 1 — Trim, Keep, and Protect the Root

See what happens with a trailing separator, with none, and with a root.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        Console.WriteLine(Path.TrimEndingDirectorySeparator("/data/reports/"));
        Console.WriteLine(Path.TrimEndingDirectorySeparator("/data/reports"));
        Console.WriteLine(Path.TrimEndingDirectorySeparator("/"));
        Console.WriteLine("[" + Path.TrimEndingDirectorySeparator("") + "]");
    }
}

How It Works

  1. One separator is removed. The slash at the end of /data/reports/ is cut off. The rest of the string is copied exactly, with no separator conversion.
  2. Nothing to trim. The second path has no trailing separator, so it is returned as it is.
  3. A root is protected. "/" is the root, so its only character stays. Trimming it would leave an empty string, which is not the root.
  4. Empty stays empty. The square brackets in the last line show that an empty input gives an empty string, not an error.

Example 2 — Get the Name of a Folder Path

GetFileName and GetDirectoryName act differently when a path ends with a separator. Trim first to get the folder name.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string folder = "/data/reports/";
        string trimmed = Path.TrimEndingDirectorySeparator(folder);

        Console.WriteLine("[" + Path.GetFileName(folder) + "]");
        Console.WriteLine("[" + Path.GetFileName(trimmed) + "]");

        Console.WriteLine(Path.GetDirectoryName(folder).Replace('\\', '/'));
        Console.WriteLine(Path.GetDirectoryName(trimmed).Replace('\\', '/'));
    }
}

How It Works

  1. A trailing separator hides the name. The last part of /data/reports/ is empty, because nothing follows the final slash. GetFileName prints empty square brackets.
  2. Trim, then ask. Without the slash, the last part is reports, which is the folder name you wanted.
  3. The same effect on the parent. For the untrimmed path, GetDirectoryName returns the whole path without its last slash, which is the folder itself. Replace covers a backslash answer on Windows.
  4. Now it is the real parent. After trimming, GetDirectoryName returns /data, the folder that contains reports.

Example 3 — Make Two Folder Paths Comparable

Two spellings of the same folder compare as different strings. Trim them first, and use EndsInDirectorySeparator to check.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string a = "/data/archive/";
        string b = "/data/archive";

        Console.WriteLine("Raw equal: " + (a == b));
        Console.WriteLine("Trimmed equal: " + (Path.TrimEndingDirectorySeparator(a) == Path.TrimEndingDirectorySeparator(b)));

        Console.WriteLine("a ends with separator: " + Path.EndsInDirectorySeparator(a));
        Console.WriteLine("Trimmed ends with separator: " + Path.EndsInDirectorySeparator(Path.TrimEndingDirectorySeparator(a)));

        Console.WriteLine("Double slash: " + Path.TrimEndingDirectorySeparator("/data//"));
    }
}

How It Works

  1. Strings differ. The two paths point to the same folder, but one has a trailing slash, so == gives False.
  2. Trim makes them equal. After both go through TrimEndingDirectorySeparator, the strings match and the comparison is True. This is the usual cleanup before using folder paths as dictionary keys.
  3. A question, not a change. EndsInDirectorySeparator answers True for a and False for the trimmed copy, and it changes nothing itself.
  4. Only one is removed. "/data//" loses just its last slash and becomes /data/. A second pass would be needed for the other one.

Frequently Asked Questions

Call Path.TrimEndingDirectorySeparator(path). It removes one trailing separator and returns the new string. On .NET Framework, which lacks it, use path.TrimEnd('/', '\\') instead.
No, only one. "/data//" becomes "/data/". If you need all of them gone, use TrimEnd with the separator characters, and be careful not to trim a root.
Because "/" is a root, and removing its separator would leave an empty path, which means something different. The same goes for C:\ on Windows.
A companion method that returns true when the path ends with a separator. It does not change the path. It is in .NET Core 3.0 and newer, like TrimEndingDirectorySeparator.
Before comparing two folder paths, before using them as dictionary keys, and before calling GetFileName or GetDirectoryName on a folder path. A trailing separator changes the answers of those methods.

Back to the method index

See all 20 Path method guides grouped by what they do.

Method index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful