PC Review


Reply
Thread Tools Rate Thread

Custom trim function - Regex

 
 
Dan Shanyfelt MCAD
Guest
Posts: n/a
 
      22nd Mar 2005
I am new to regex, but I am assuming it provides a better way to do
what I need.

I want to write a custom trim functions that will remove all
occurrences of a non-whitespace character from the beginning or end of
a functions

TrimEnd(string Original, string Pad)
TrimStart(string Original, string Pad)
-Where Pad is a string with a length of 1.

So, TrimEnd("12345XXX", "X") would give "12345".

I know I can do this in a less elegant (and slower) way without Regex,
but this operation will happen frequently.

I am starting to learn Regex, but could use the help to get this
implementation detail knocked out.

TIA,
Dan

 
Reply With Quote
 
 
 
 
=?Utf-8?B?SmFyb2Q=?=
Guest
Posts: n/a
 
      22nd Mar 2005
> I am new to regex, but I am assuming it provides a better way to do
> what I need.
>
> I want to write a custom trim functions that will remove all
> occurrences of a non-whitespace character from the beginning or end of
> a functions
>

Do you know that for regex non-whitespace mean also letters and numbers ?
You are probably looking for:

^(\W)+ - this will find anything like #$%^@@#( also spaces )

and if want start from the back :
$(\W)+

I believe that this site will be extremly helpful :
http://www.roblocher.com/technotes/regexp.aspx

You can test what it wrote above on your sample string to see if it's what
you was looking for.
Jarod

 
Reply With Quote
 
Daniel Shanyfelt
Guest
Posts: n/a
 
      23rd Mar 2005
Thanks Jarod.

That wasn't exactly what I was looking for, but it put me on the right
track.

so..

private string CustomTrimEnd(
string Original,
string TrimChar)
{
Regex r = new Regex(
string.Format("[{0}]+$", TrimChar),
RegexOptions.IgnoreCase);
return r.Split(Original)[0];
}

private string CustomTrimStart(
string Original,
string TrimChar)
{
Regex r = new Regex(
string.Format("^[{0}]+", TrimChar),
RegexOptions.IgnoreCase);
return r.Split(Original)[1];
}

//code from a quick test app

When I move this into the app the same trim char will be used many
times. So, I won't be recreating the pattern every time (just on init).

Again, thanks for the help.

-Dan




*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function =Trim() Dowitch Microsoft Excel Worksheet Functions 5 29th Apr 2009 10:22 PM
Function =Trim() Dowitch Microsoft Excel Worksheet Functions 0 29th Apr 2009 06:30 PM
regex.replace and trim Pascal Microsoft VB .NET 3 6th Nov 2006 01:47 AM
trim spaces before evaluating regex John A Grandy Microsoft C# .NET 10 30th Sep 2005 10:57 AM
Difference between Trim function and Trim member Sascha Herpers Microsoft VB .NET 8 7th Apr 2005 08:57 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:03 AM.