473,386 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Custom trim function - Regex

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

Nov 17 '05 #1
2 3923
> 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

Nov 17 '05 #2
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!
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mikael Hellström | last post by:
Hi all, i need to use the trim function trim("str","?") My question are. How do i remove the "return sign" from a string?? Regards Mikael
6
by: Dominic | last post by:
Here is the general problem: Information is taken from a form (consisting of text boxes, drop down lists et cetera) and submitted to an Access database. Before the data is inserted into the...
2
by: SK | last post by:
Trim function doesnt actually trim the spaces.Can anyone help
5
by: Dave Bovey | last post by:
I created a church database in Access 97 that contain the typical Name, Address, and Phone Number information fields. I had a number of different reports to print query data in different formats. ...
7
by: Sascha Herpers | last post by:
Hi, what is the difference between the trim function and the trim String-member? As far as I see it, both return the trimmed string and leave the original string unaltered. Is any of the two...
31
by: rkk | last post by:
Hi, I've written a small trim function to trim away the whitespaces in a given string. It works well with solaris forte cc compiler, but on mingw/cygwin gcc it isn't. Here is the code: char...
1
by: shalini jain | last post by:
Hi, I want to know if the syntax of trim function that i have written is correct or not? if(width!=null && width!="" && width!=trim(imgWidth)) { alert('hello'); } my code is not proceeding...
4
by: ramaswamynanda | last post by:
Hello All, I have developed an application using Ms Access 2000. My client has Access 2003. I shipped him the code yesterday and he got an error hile running the application via the mdb file...
3
by: BlackMustard | last post by:
hi all, i am having problems using the Trim() function in a program written in VB6. i'm getting my strings from reading a textfile line by line, but regardless of the trim function a couple of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.