Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replace method 1

Status
Not open for further replies.

jshurst

Programmer
Joined
Oct 27, 2004
Messages
1,158
Location
US
I would like strip certain characters out of a string. I can do this easily for one character.
Code:
Dim a as string
a = txtName.text.replace("*","")
But what if I want to replace more than one character (a list of characters)? Should I do this...
Code:
Dim a as string
a = txtName.text.replace("*","").replace("#","")...

Or perhaps loop through somehow? I guess I'm asking for the best way to do this.

Thanks.
 
I'm wondering if you could use a Regular Expression to do this...

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Here's a simple Regex that replaces all of the characters between the brackets with "". Should be simple enough to convert to vb.
Code:
[COLOR=blue]public[/color] [COLOR=blue]static[/color] [COLOR=blue]string[/color] CleanMyString([COLOR=blue]string[/color] inputText)
        {
            Regex regex = [COLOR=blue]new[/color] Regex(@"[@!#$%^&*]",RegexOptions.IgnoreCase);]
            [COLOR=blue]string[/color] cleanString = regex.Replace(inputText,"");
            [COLOR=blue]return[/color] cleanString;
        }
 
Thanks a lot that works perfectly.
 
Just converted the Above code in VB.NET in case anyone needs it. It is not tested, so I hope it works.

Code:
imports system.text.regularexpressions

public function CleanMyString(byval inputText as string) as string
  Dim MyRegex as new Regex("[@!#$%^&*", RegexOptions.IgnoreCase)
  Dim cleanString as strng = MyRegex.Replace(inputText, "")
  return cleanString
End function
 
You may also want to check out some of the Regular Expression articles at 4guysFromRolla:



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top