Click here to Skip to main content
15,886,806 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

StringBuilder Extensions

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
30 Nov 2010CPOL 11.8K   2   3
A note about StringBuilder Extensions

Forgive me for sounding a little pedantic, but wouldn't you be able to use the LTrim and RTrim functions within the Trim function? (You'll have to forgive my syntax, I'm more of a Delphi guy myself). "Re-writing" the same code in separate places tends to cause issues when you need to update it, or at least that's what I've found when working on my code Smile | :) .

C#
/// <summary>
/// Trim spaces around string
/// </summary>
/// <param name="sb"></param>
/// <returns></returns>
public static StringBuilder Trim(this StringBuilder sb)
{
    sb = LTrim(sb);
    sb = RTrim(sb);
    return sb;
}

Thanks,
Glen Vlotman

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
South Africa South Africa
I've been coding for the better part now of about 15 years, starting in the good old Turbo Pascal 7 days, when I made my very first computer game ( my own version of Reversi / Othello) and I was hooked, through to Delphi 2009 (and higher, just didn't use it that much).

For the last 3 years or so I have been off and on using C#/ASP.NET (and even more recently Entity Framework and MVC) and I think it is TOTALLY awesome. Having said that though, I must admit that I will always be a Delphi guy at heart. I've dabbled in a few other languages and am always on the lookout to keep in touch with what's new in the software development scene.

When I am not coding, I keep myself busy writing short stories[^], partying hard, singing at karaoke (don't judge me!), trying to learn how to play the guitar and generally helping others wherever I can.

Comments and Discussions

 
GeneralThis looks good. One suggestion might be to have an: [RTr... Pin
Ctznkane29-Dec-11 9:18
Ctznkane29-Dec-11 9:18 
GeneralTrue! I wrote Trim() first and then simplified it for RTrim(... Pin
nguyenphuphi8-Dec-10 11:53
nguyenphuphi8-Dec-10 11:53 
GeneralReason for my vote of 5 my thoughts exactly! Pin
Karthik. A3-Dec-10 3:50
Karthik. A3-Dec-10 3:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.