Search The Blog
About this site

@RoyOsherove

Subscribe!

This site aims to connect all the dots of my online activities - from tools, books blogs and twitter accounts, to upcoming conferences, engagements and user group talks.

from 5whys.com
Twitter: @RoyOsherove
My Book: The Art of Unit Testing
Latest Posts
« Beware of Vs.Net plugins and wizards bearing gifts | Main | A new low in television reached »
Friday
Apr302004

TrimEnd() Vs. SubString()

Many developers have had to write a loop that writes out a comma separated value list. There's always the trick of how you make that last comma get removed from the end of that string. Usually, at the end of the loop, many developers will do something like this to remove the last char in the string:
 
out = out.Substring( 0, out.LastIndexOf( ',' )
 
However, there's a cleaner way to accomplish this task:
 
out = out.TrimEnd(',')
 
TrimeEnd can receive a char array so you can remove any unwanted chars from the end easily. The code is also more readable. And yes, there's a TrimStart() to match that function.

PrintView Printer Friendly Version

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>