How to remove non alphanumeric characters (special characters) from a string in C# ?


This content was proof-read by Grammarly. You can Try Grammary for FREE

One of the simplest way to remove non alphanumeric characters from a string is using the regular expressions . Below is a sample code snippet that demonstrates how to delete the non alphanumeric characters from a string in C#.

How to remove non alphanumeric characters (special characters) from a string in C# ?

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
namespace AbundantCode
{
    internal class Program
    {

        private static void Main(string[] args)
        {

            string input = "Welcome to http://www.Abundantcode.com";
            string Output = GetNonNumericData(input);
            Console.WriteLine(Output);
            Console.ReadLine();

        }
        //How to remove non alphanumeric characters (special characters) from a string in C# ?
        public static string GetNonNumericData(string input)
        {
            Regex regex = new Regex("[^a-zA-Z0-9]");
            return regex.Replace(input, "");
        }

    }

}
How to remove non alphanumeric characters (special characters) from a string in C# ?
Newsletter Signup

Subscribe to our weekly newsletter below and never miss the latest product or an exclusive offer.

Newsletter Signup

Subscribe to our weekly newsletter below and never miss the latest product or an exclusive offer.

%d bloggers like this: