aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# Regex to Replace All Special Characters with Space in String

Sep 26, 2013

Here I will explain how to use regular expression or regex in c# to replace all special characters with space in string using c#,


In previous articles I explained constructors in c#, polymorphism example in c#, delegates in c# with example, Difference b/w compile time and runtime polymorphism, sealed classes in and many articles relating to c#,, code snippets. Now I will explain how to replace all special characters in string with space in c#,

C# Code to replace special characters with spaces

If we want to replace all special characters in string with spaces we need to write code like as shown below

using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication3
class Program
static void Main(string[] args)
string str = "";
string replacestr= Regex.Replace(str, "[^a-zA-Z0-9_]+", " ");

Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim str As String = ""
Dim replacestr As String = Regex.Replace(str, "[^a-zA-Z0-9_]+", " ")
End Sub
End Module
If we run above code we will get output like as shown below


welcome to aspdotnet suresh com

I hope it helps you to solve your problem. Happy coding…

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Anonymous said...


muay thai kursu said...

Its very helpful. thx.

Unknown said...

Very helpful code.....
But there is a small mistake in C# code. Third parameter (MatchEvaluator) should be " " instead of "-".
VB.Net code is correct.

Anonymous said...

I really like the post..

Chandan said...

Sir i have to insert special character in ms access database like 44'12 using c# plz help

Unknown said...

It is very Helpfull code sir.
But i want whenever we insert at a time multiple special characters on that time it is taken single space but,i want how many special characters is there that much of spaces we want.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.