friendsterTalk - Friendster Forum

friendsterTalk - Friendster Forum

Welcome guest! Please Login or Register.

#1  2008-11-11 17:29:33

creamownedz
 active server protocol
creamownedz's display avatar
» FriendsterTalker
PsychoCoder
Class-S
Location: Ftalk & MCTF
Registered: 2008-03-22
Posts: 115
Last visit: Yesterday
Reputation: 7

Remove trailing & leading spaces with regular expressions [c#]

This is a snippet I use for removing leading and trailing whitespace in a string


Instructions: Need a reference to System.Text.RegularExpressions. Pass the method your string and it will trim it for you


Code:

//Namespace Reference
using System.Text.RegularExpressions;

/// <summary>
/// Method for removing trailing and leadingt whitespace from a string
/// </summary>
/// <param name="str">The string to trim</param>
/// <returns></returns>
public string TrimString(string str)
{
    try
    {
        string pattern = @"^[ \t]+|[ \t]+$";
        Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);
        str = reg.Replace(str, "");
        return str;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

...Giving of reputation are highly appreciating for me...
Computers don't make errors what they do they do on purpose
...computer is my expertise...
....an addicted coder....
....don't ripp all my tutorials....

Search Friendstertalk

Board footer

FriendsterTalk is not affiliated with Friendster.com
Copyright © 2002–2009 PunBB

[ 8 queries - 3.177 second ]

Pay Per Click Ads by pay per click advertising by Kontera

FriendsterTalk.com x

Welcome to FriendsterTalk! You'll need to login in order to fully use all the features and view all the sections of this site.

Please register if you're not yet a member. =)