Search
On this page
Archives
RSS 2.0 Categories
Blogroll
Disclaimer
Powered by: newtelligence dasBlog 2.0.7226.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
Send mail to the author(s) E-mail
A Better Way to Remove a Trailing Slash from a Path#
C#

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[])

 

Friday, January 18, 2008 5:32:41 AM (GMT Standard Time, UTC+00:00)
  Comments [0]  | 
All content © 2010 , Rik Robinson