Friday, 27 October 2017

Replace all Special Character From String In C# Using Regex / RegularExpression


Introduction
In this article we will learn how to use regular expression/regex in c#  and use regex to replace all special characters with space in string using c#. 

Previous Updates
In previous articles we have learnt Showing Chart With Database In Asp.Net Using C#. Get TextBox , Dropdown, CheckBox  control Values In Aspx.cs Page From User Control Using C#.

Description:
If you think you need to remove all the special character from string looks tough to do or need some special logic to implement. But here you dont need to worry about anything , Regex will do all the work for you. A simple line of code of regex solve your many problems.

 Maintain state of dynamic added userConrol in asp.net using c#.

using System.Text.RegularExpressions;   //paste this line in namespace


class Program
{
  static void Main(string[] args)
 {
    string myStr = "You-Are@visiting@-GurujiPoint@@.com";
    string output= Regex.Replace(myStr, "[^a-zA-Z0-9_]+"" ");
    Console.WriteLine(output);
    Console.ReadLine();
   }
}
Insert Only Numeric values in Asp Textbox using Regex.

Output:
You Are visiting GurujiPoint.com


No comments:

Post a Comment