Patrick Desjardins Blog
Patrick Desjardins picture from a conference

How to remove trailing slash in C#

Posted on: 2012-06-26

I see often people using substring to remove the last character if this one is a slash in a path or url. This can if misused lead to an error because maybe the string is empty and doesn't have count()-1 over 0.

Also, this require to have a conditional statement to verify if the string contain at its latest position a slash.

A cleaner way to proceed with this kind of string clean up it's to use the TRIM function of the string. The method TrimEnd let you specify an array of char that you want to remove at the end of the specified string.

Here is an example:

string fileName = "Test/"; 
fileName= fileName.TrimEnd(new[] { '/' }); 

With this method you can also specify multiple characters so you may want to remove all slash or backslash using the array.

string fileName = "Test\\\\"; 
fileName= fileName.TrimEnd(new[] { '/', '\\\\' }); 

Do not forget that this method will trim more than only the last character but all ending characters. This mean that a string ending with two slashes will see both slashes removed. I think it's even better!