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, "");
}
}
}
Leave a Reply