Remove Special characters from a string - Regex c#

733
Views
2
Answers

How can I remove special characters from a string and replace it with - or + using

Regex.Replace

I have to allow only uppercase/lowercase a-z and 0-9.

edit | flag
Saranya Gokul
Asked on: Feb 02, 2012 at 9:01PM

2 Answers

120
7
120

Try this..

 string str = "Error res//ieframe.dll/http_500_webOC.htm#http//servername";

str
= Regex.Replace(str, "[^a-zA-Z0-9]+", "-", RegexOptions.Compiled);

[^a-zA-Z0-9] says to escape these characters in the string without replacing.

RegexOptions.Compiled - You can tell it to compile when it runs first time and the subsequent calls will be faster.

edit | flag
Sam Bells
Answered on: Feb 02, 2012 at 9:08PM
140
0
140

Use Regex.Replace

edit | flag
Ron McDonald
Answered on: Jul 20, 2012 at 11:41PM

Post your Answer

Search

Welcome to Ask Amoeba!
This is 100% free and interactive site for sharing professional Questions and Answers, Opensource projects, Interview questions.
Learners, beginners, Experts stop and share your knowledge and ideas!

Browse Categories

Browse Tags