Friday, January 18, 2008 12:32 AM
r2 musings - rants, raves, and research (mostly on .NET topics) from rik robinson
A Better Way to Remove a Trailing Slash from a Path
How many times have you written this to trim a trailing slash from a path:
if (myPath.EndsWith("\\"))
/* or "/" in the case of a URI */
myPath = myPath.Substring(0,
myPath.Length-1);
Next time, try this:
myPath = myPath.TrimEnd(new char[]{'\\',
'/'});
>>
>
In
addition to String.TrimStart() and String.TrimEnd(), there is an overload on String.Trim()
that accepts a character array. There are good examples of usage in the MSDN
Library. Here
are the links:
System.String Trim(char[]) TrimStart(char[]) TrimEnd(char[])