By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,136 Members | 1,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,136 IT Pros & Developers. It's quick & easy.

VB.NET String Manipulation similar to RegularExpresion

P: n/a
Hi,

Does anyone knows any good code for string manipulation similar to
RegularExpresion?

I might get a value as string in a different format. Example:

20/02/2006 or 20,02,2006 or 20.02.2006 etc...

And I want to replace the /,.etc character with - (as 20-02-2006)

The VB.NET replace operation can replace single character at a time. What I
need is similar to RegularExpresion that I might code a sinle line as:

Dim mySTR as String = ""
mySTR = CStr(TableTAK.Rows(i))

Dim myCurDate as String = ""
myCurDate = mySTR.xReplace("/,.", "-")

The function that I need (xReplace) will replace any character "/,." in
mySTR with character "-".

The xReplace function might allow me to assign any character that I want to
replace. Not just 3 character that I desribe in above.

I know I have to create a function to do but I am looking for much faster
way doing that like in RegularExpresion.

I thank you in advance for reading my post.

Rgds,
GC

Aug 2 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Niyazi <Ni****@discussions.microsoft.comwrote:
Does anyone knows any good code for string manipulation similar to
RegularExpresion?
Any reason not to just use System.Text.RegularExpressions?

<snip>
I know I have to create a function to do but I am looking for much faster
way doing that like in RegularExpresion.
I suspect that for reasonably small numbers of replacement characters,
using the straight String.Replace method will be faster than using
regular expressions, actually. If you really care about performance,
you should certainly measure it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 2 '06 #2

P: n/a
Hi Jon,

The String.Replace it allows me to replace one character at a time and I
have to code 3 line for each character. What I was looking for to replace the
more than one character if its found in string with the second character.

String.Replace("/|*|.|,", "-")

As you may see I need to replace the / * . , character with -
But String.Replace doesnot allow me to use more than one character. On the
other hand the RegularExpresion cannot allow me to use some character that I
need to be replaced.

So I guess I have to use my own function to do that. I wish I can manipulate
with overloading the String.Replace but unfortunatly I don't know how.

Thank you for your input.

Rgds,
GC

"Jon Skeet [C# MVP]" wrote:
Niyazi <Ni****@discussions.microsoft.comwrote:
Does anyone knows any good code for string manipulation similar to
RegularExpresion?

Any reason not to just use System.Text.RegularExpressions?

<snip>
I know I have to create a function to do but I am looking for much faster
way doing that like in RegularExpresion.

I suspect that for reasonably small numbers of replacement characters,
using the straight String.Replace method will be faster than using
regular expressions, actually. If you really care about performance,
you should certainly measure it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 3 '06 #3

P: n/a
Niyazi <Ni****@discussions.microsoft.comwrote:
The String.Replace it allows me to replace one character at a time and I
have to code 3 line for each character. What I was looking for to replace the
more than one character if its found in string with the second character.

String.Replace("/|*|.|,", "-")
You've just demonstrated why using regular expressions is dangerous -
more later.
As you may see I need to replace the / * . , character with -
But String.Replace doesnot allow me to use more than one character. On the
other hand the RegularExpresion cannot allow me to use some character that I
need to be replaced.

So I guess I have to use my own function to do that. I wish I can manipulate
with overloading the String.Replace but unfortunatly I don't know how.
Yes, you'd have to write your own helper method which went through an
array of characters and called String.Replace on each of them. The good
thing about this is that it's incredible simple. The bad thing is that
as the number of characters to replace increases, so does the time
taken.

The bad thing about regular expressions is that they're complicated.
The expression /|*|.|, doesn't match a single slash, a single star, a
single dot or a single comma - because dot and star have specific
meanings in regular expressions. You need to be very, very careful.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 3 '06 #4

P: n/a
Niyazi wrote:
As you may see I need to replace the / * . , character with -
But String.Replace doesnot allow me to use more than one character. On the
other hand the RegularExpresion cannot allow me to use some character that I
need to be replaced.
Sure it does.

Regex.Replace(Text, "[/*.]", "-");

// match any of the three characters /, *, or .
// replace all matches with "-".

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.
Aug 3 '06 #5

P: n/a
Hi Jon Shemitz,

Thank you for your input. It did worked when use [] character.
I thank you very much.

Rgds,
GC

"Jon Shemitz" wrote:
Niyazi wrote:
As you may see I need to replace the / * . , character with -
But String.Replace doesnot allow me to use more than one character. On the
other hand the RegularExpresion cannot allow me to use some character that I
need to be replaced.

Sure it does.

Regex.Replace(Text, "[/*.]", "-");

// match any of the three characters /, *, or .
// replace all matches with "-".

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.
Aug 7 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.