Multiple Occurances Of Value In String

G

Guest

I am wanting to find any instance in a string where more then on occurance of a value occurs and replace it with a single occurance of the value. Specifically I want to search a text field and find any time where more then a single ' (tic) occurs in succession, and replace it with a single ' (tic).

Can this be done with a RegularExpression? Any example of how to do this?
 
M

Martin Marinov

You don't need to use regular expressions.
Try this

private string ReplaceString ( string Input )

{

if ( Input.IndexOf("''") != -1 )

Input = ReplaceString( Input.Replace("''", "'") );

return Input;

}

where Input is the string for search the ' symbol



Regards
Martin

Jim Heavey said:
I am wanting to find any instance in a string where more then on occurance
of a value occurs and replace it with a single occurance of the value.
Specifically I want to search a text field and find any time where more then
a single ' (tic) occurs in succession, and replace it with a single ' (tic).
 
B

Benjamin Bittner

Hallo Jim
Jim Heavey said:
I am wanting to find any instance in a string where more then on occurance
of a value occurs and replace it with a single occurance of the value.
Specifically I want to search a text field and find any time where more then
a single ' (tic) occurs in succession, and replace it with a single ' (tic).
Can this be done with a RegularExpression? Any example of how to do this?
Do you mean that u want to do that with every possible value? so if youd
have a string like that:

aa++bb77jjkk..--hhjjkkll

it would return

a+b7jk.-hjkl

if you just want to replace '' with ' you could do something like this:

txtUser.Text.Replace("''", "'")

or for +

txtUser.Text.Replace("++", "+")

if you tryin to prevent a user to use sql injection in some of your forms, i
suggest you construct more security functions than that.

regards benni
 
M

Martin Marinov

It won't be a problem
i've tested it with 3, 4,5 even 7 tic's and everything works fine ( if you
see the code the function is calling recursive to check if there is more
double tic's after replacing )

Regards
Martin

Jim Heavey said:
This will work fine when you know you have '', but what about when you
have 3 or 4 tic's? I know that I could write 10 replace statements, but I
was thinking there was an easier way...sy with a regular expression?
 
M

Martin Marinov

But if you still want to use regular expressions you can use this

System.Text.RegularExpressions.Regex r = new
System.Text.RegularExpressions.Regex(@"'+");
textBox2.Text = r.Replace(textBox1.Text, "'");



Regards
Martin

Jim Heavey said:
This will work fine when you know you have '', but what about when you
have 3 or 4 tic's? I know that I could write 10 replace statements, but I
was thinking there was an easier way...sy with a regular expression?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top