简体   繁体   中英

Removing non-ASCII characters from string

I am trying to strip non-ASCII character from strings I am reading from a text file and can't get it to do so. I checked some of the suggestions from posts in SO and other sites, all to no avail.

This is what I have and what I have tried:

String in text file:

2021-03-26 10:00:16:648|2021-03-26 10:00:14:682|MPE->IDC|[10.20.30.40:41148]|203, ?  ?'F?~?^?W?|?8wL?i??{?=kb ?   Y  R?

String read from the file:

"2021-03-26 10:00:16:648|2021-03-26 10:00:14:682|[10.20.30.40:41148]|203,\u0016\u0003\u0001\0?\u0001\0\0?\u0003\u0001'F?\u001e~\u0018?^?W\u0013?|?8wL\v?i??{?=kb\t?\tY\u0005\0\0R?"

Methods to get rid of non-ASCII characters:

Regex reAsciiPattern = new Regex(@"[^\u0000-\u007F]+");  // Non-ASCII characters
sLine = reAsciiPattern.Replace(sLine, "");   // remove non-ASCII chars

Regex reAsciiPattern2 = new Regex(@"[^\x00-\x7F]+");  // Non-ASCII characters
sLine = reAsciiPattern2.Replace(sLine, "");   // remove non-ASCII chars

string asAscii = Encoding.ASCII.GetString(
    Encoding.Convert(
        Encoding.UTF8,
        Encoding.GetEncoding(
            Encoding.ASCII.EncodingName,
            new EncoderReplacementFallback(string.Empty),
            new DecoderExceptionFallback()
            ),
        Encoding.UTF8.GetBytes(sLine)
    )
);

What am I missing?

Thanks.

This can be done without a Regex using a loop and a StringBuilder :

var sb = new StringBuilder();
foreach(var ch in line) {
   //printable Ascii range
   if (ch >= 32 && ch < 127) { 
       sb.Append(ch);
   } 
}

line = sb.ToString();

Or you can use some LINQ:

line = string.Concat(
  line.Where(ch => ch >= 32 && ch < 127)
);

If you must do this with Regex then the following should suffice (again this keeps printable ASCII only)

line = Regex.Replace(line, @"[^\u0020-\u007e]", "");

Try It Online

If you want all ASCII (including non-printable) characters, then modify the tests to

ch <= 127 // for the loops
@"[^\u0000-\u007f]" // for the regex

您可以使用以下正则表达式去除所有不可打印的字符。

Regex.Replace(sLine, @"[^\u0020-\u007E]+", string.Empty);

This is what worked for me based on a post here

using System.Text.RegularExpressions;
...

Regex reAsciiNonPrintable = new Regex(@"\p{C}+");  // Non-printable characters
string sLine;

using (StreamReader sr = File.OpenText(Path.Combine(Folder, FileName)))
{
    while (!sr.EndOfStream)
    {
        sLine = sr.ReadLine().Trim();

        if (!string.IsNullOrEmpty(sLine))
        {
            Match match = reAsciiNonPrintable.Match(sLine);

            if (match.Success)
                continue;    // skip the line
            ...
        }
        ...
    }
    ....
}

Since a string is an IEnumerable<char> where each char represents one UTF-16 code unit (possibly a surrogate), you can also do:

var ascii = new string(sLine.Where(x => x <= sbyte.MaxValue).ToArray());

Or if you want only printable ASCII:

var asciiPrintable = new string(sLine.Where(x => ' ' <= x && x <= '~').ToArray());

I realize now that this is mostly a duplicate of pinkfloydx33's answer, so go and upvote that.

If the string contains accented letters, the result can depend on the normalization, so compare:

var sLine1 = "olé";
var sLine2 = sLine1.Normalize(NormalizationForm.FormD);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM