VB.NET String Manipulation similar to RegularExpresion

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Niyazi said:
Does anyone knows any good code for string manipulation similar to
RegularExpresion?

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

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.
 
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
 
Niyazi said:
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.
 
Niyazi said:
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 "-".
 
Hi Jon Shemitz,

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

Rgds,
GC

Jon Shemitz said:
Niyazi said:
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 "-".
 
Back
Top