c.StringExtensions.StripSpecialCharacters

PHOTO EMBED

Mon Sep 13 2021 14:24:31 GMT+0000 (Coordinated Universal Time)

Saved by @rick_m #c#

using System.Text;
using System.Text.RegularExpressions;

public static class StringExtensions
{
    public static string StripSpecialCharacters(this string s)
    {
        if (string.IsNullOrEmpty(s))
            return string.Empty;

        Regex r = new Regex(@"\s+");//remove all whitespace
        s = r.Replace(s, " ");	// to a single space

        MatchCollection mc = Regex.Matches(s, @"[A-Za-z0-9]|\s+", RegexOptions.IgnoreCase);
        s = string.Empty;
        foreach (Match m in mc)
        {
            s += m.ToString();
        }

        return s;
    }
  
}
content_copyCOPY

somestring.StripSpecialCharacters();