Skip to main content

Remove characters that are not printable ASCII characters from a string in C#. The following TrimNonAscii extension method removes non-printable ASCII characters from a string.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

    public static class StringStuff
        public static string TrimNonAscii(this string value)
            string pattern = "[^ -~]*";
            Regex reg_exp = new Regex(pattern);
            return reg_exp.Replace(value, "");