简体   繁体   English

如何从字符串中去除非 ASCII 字符? (在 C# 中)

[英]How can you strip non-ASCII characters from a string? (in C#)

How can you strip non-ASCII characters from a string?如何从字符串中去除非 ASCII 字符? (in C#) (在 C# 中)

["
string s = "søme string";
s = Regex.Replace(s, @"[^\u0000-\u007F]+", string.Empty);

Here is a pure .NET solution that doesn't use regular expressions:这是一个不使用正则表达式的纯 .NET 解决方案:

string inputString = "Räksmörgås";
string asAscii = Encoding.ASCII.GetString(
    Encoding.Convert(
        Encoding.UTF8,
        Encoding.GetEncoding(
            Encoding.ASCII.EncodingName,
            new EncoderReplacementFallback(string.Empty),
            new DecoderExceptionFallback()
            ),
        Encoding.UTF8.GetBytes(inputString)
    )
);

It may look cumbersome, but it should be intuitive.它可能看起来很麻烦,但它应该是直观的。 It uses the .NET ASCII encoding to convert a string.它使用 .NET ASCII 编码来转换字符串。 UTF8 is used during the conversion because it can represent any of the original characters.在转换过程中使用 UTF8,因为它可以表示任何原始字符。 It uses an EncoderReplacementFallback to to convert any non-ASCII character to an empty string.它使用 EncoderReplacementFallback 将任何非 ASCII 字符转换为空字符串。

["

我相信 MonsCamus 的意思是:<\/p>

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

If you want not to strip, but to actually convert latin accented to non-accented characters, take a look at this question: How do I translate 8bit characters into 7bit characters?如果您不想剥离,而是要将拉丁重音字符转换为非重音字符,请查看以下问题:如何将 8 位字符转换为 7 位字符? (ie Ü to U) (即 Ü 到 U)

["

Inspired by philcruz's Regular Expression solution<\/a> , I've made a pure LINQ solution<\/i>philcruz 的正则表达式解决方案的<\/a>启发,我做了一个纯 LINQ 解决方案<\/b><\/p>

public static string PureAscii(this string source, char nil = ' ')
{
    var min = '\u0000';
    var max = '\u007F';
    return source.Select(c => c < min ? nil : c > max ? nil : c).ToText();
}

public static string ToText(this IEnumerable<char> source)
{
    var buffer = new StringBuilder();
    foreach (var c in source)
        buffer.Append(c);
    return buffer.ToString();
}

no need for regex.不需要正则表达式。 just use encoding...只需使用编码...

sOutput = System.Text.Encoding.ASCII.GetString(System.Text.Encoding.ASCII.GetBytes(sInput));

I came here looking for a solution for extended ascii characters, but couldnt find it.我来这里寻找扩展 ascii 字符的解决方案,但找不到。 The closest I found is bzlm's solution .我找到的最接近的是bzlm's solution But that works only for ASCII Code upto 127(obviously you can replace the encoding type in his code, but i think it was a bit complex to understand. Hence, sharing this version).但这仅适用于最高 127 的 ASCII 代码(显然您可以替换他代码中的编码类型,但我认为理解起来有点复杂。因此,分享这个版本)。 Here's a solution that works for extended ASCII codes ie upto 255 which is the ISO 8859-1这是适用于扩展 ASCII 代码的解决方案,即高达 255 ,即ISO 8859-1

It finds and strips out non-ascii characters(greater than 255)它发现并去除非ASCII字符(大于255)

Dim str1 as String= "â, ??î or ôu🕧� n☁i✑💴++$-💯♓!🇪🚑🌚‼⁉4⃣od;/⏬'®;😁☕😁:☝)😁😁///😍1!@#"

Dim extendedAscii As Encoding = Encoding.GetEncoding("ISO-8859-1", 
                                                New EncoderReplacementFallback(String.empty),
                                                New DecoderReplacementFallback())

Dim extendedAsciiBytes() As Byte = extendedAscii.GetBytes(str1)

Dim str2 As String = extendedAscii.GetString(extendedAsciiBytes)

console.WriteLine(str2)
'Output : â, ??î or ôu ni++$-!‼⁉4od;/';:)///1!@#$%^yz:

Here's a working fiddle for the code是代码的工作小提琴

Replace the encoding as per the requirement, rest should remain the same.根据要求更换编码,其余保持不变。

["

I found the following slightly altered range useful for parsing comment blocks out of a database, this means that you won't have to contend with tab and escape characters which would cause a CSV field to become upset.<\/i>我发现以下稍微更改的范围对于从数据库中解析注释块很有用,这意味着您不必与制表符和转义符抗衡,这会导致 CSV 字段变得不安。<\/b><\/p>

parsememo = Regex.Replace(parsememo, @"[^\u001F-\u007F]", string.Empty);

This is not optimal performance-wise, but a pretty straight-forward Linq approach:这不是最佳性能,而是一种非常直接的 Linq 方法:

string strippedString = new string(
    yourString.Where(c => c <= sbyte.MaxValue).ToArray()
    );

The downside is that all the "surviving" characters are first put into an array of type char[] which is then thrown away after the string constructor no longer uses it.缺点是所有“幸存的”字符首先被放入一个char[]类型的数组中,然后在string构造函数不再使用它之后将其丢弃。

I used this regex expression:我使用了这个正则表达式:

    string s = "søme string";
    Regex regex = new Regex(@"[^a-zA-Z0-9\s]", (RegexOptions)0);
    return regex.Replace(s, "");

I use this regular expression to filter out bad characters in a filename.我使用这个正则表达式来过滤掉文件名中的坏字符。

Regex.Replace(directory, "[^a-zA-Z0-9\\:_\- ]", "")

That should be all the characters allowed for filenames.这应该是文件名允许的所有字符。

public string ReturnCleanASCII(string s)
    {
        StringBuilder sb = new StringBuilder(s.Length);
        foreach (char c in s)
        {
            if ((int)c > 127) // you probably don't want 127 either
                continue;
            if ((int)c < 32)  // I bet you don't want control characters 
                continue;
            if (c == '%')
                continue;
            if (c == '?')
                continue;
            sb.Append(c);
        }
        return sb.ToString();
    }
["

If you want a string with only ISO-8859-1 characters and excluding characters which are not standard, you should use this expression :<\/i>如果你想要一个只有 ISO-8859-1 字符并且不包括非标准字符的字符串,你应该使用这个表达式:<\/b><\/p>

var result = Regex.Replace(value, @"[^\u0020-\u007E\u00A0-\u00FF]+", string.Empty);

Necromancing.死灵术。
Also, the method by bzlm can be used to remove characters that are not in an arbitrary charset, not just ASCII:此外,bzlm 的方法可用于删除不在任意字符集中的字符,而不仅仅是 ASCII:

// https://en.wikipedia.org/wiki/Code_page#EBCDIC-based_code_pages
// https://en.wikipedia.org/wiki/Windows_code_page#East_Asian_multi-byte_code_pages
// https://en.wikipedia.org/wiki/Chinese_character_encoding
System.Text.Encoding encRemoveAllBut = System.Text.Encoding.ASCII;
encRemoveAllBut = System.Text.Encoding.GetEncoding(System.Globalization.CultureInfo.InstalledUICulture.TextInfo.ANSICodePage); // System-encoding
encRemoveAllBut = System.Text.Encoding.GetEncoding(1252); // Western European (iso-8859-1)
encRemoveAllBut = System.Text.Encoding.GetEncoding(1251); // Windows-1251/KOI8-R
encRemoveAllBut = System.Text.Encoding.GetEncoding("ISO-8859-5"); // used by less than 0.1% of websites
encRemoveAllBut = System.Text.Encoding.GetEncoding(37); // IBM EBCDIC US-Canada
encRemoveAllBut = System.Text.Encoding.GetEncoding(500); // IBM EBCDIC Latin 1
encRemoveAllBut = System.Text.Encoding.GetEncoding(936); // Chinese Simplified
encRemoveAllBut = System.Text.Encoding.GetEncoding(950); // Chinese Traditional
encRemoveAllBut = System.Text.Encoding.ASCII; // putting ASCII again, as to answer the question 

// https://stackoverflow.com/questions/123336/how-can-you-strip-non-ascii-characters-from-a-string-in-c
string inputString = "RäksmörПривет, мирgås";
string asAscii = encRemoveAllBut.GetString(
    System.Text.Encoding.Convert(
        System.Text.Encoding.UTF8,
        System.Text.Encoding.GetEncoding(
            encRemoveAllBut.CodePage,
            new System.Text.EncoderReplacementFallback(string.Empty),
            new System.Text.DecoderExceptionFallback()
            ),
        System.Text.Encoding.UTF8.GetBytes(inputString)
    )
);

System.Console.WriteLine(asAscii);

AND for those that just want to remote the accents:对于那些只想远离口音的人:
(caution, because Normalize != Latinize != Romanize) (注意,因为 Normalize != Latinize != Romanize)

// string str = Latinize("(æøå âôû?aè");
public static string Latinize(string stIn)
{
    // Special treatment for German Umlauts
    stIn = stIn.Replace("ä", "ae");
    stIn = stIn.Replace("ö", "oe");
    stIn = stIn.Replace("ü", "ue");

    stIn = stIn.Replace("Ä", "Ae");
    stIn = stIn.Replace("Ö", "Oe");
    stIn = stIn.Replace("Ü", "Ue");
    // End special treatment for German Umlauts

    string stFormD = stIn.Normalize(System.Text.NormalizationForm.FormD);
    System.Text.StringBuilder sb = new System.Text.StringBuilder();

    for (int ich = 0; ich < stFormD.Length; ich++)
    {
        System.Globalization.UnicodeCategory uc = System.Globalization.CharUnicodeInfo.GetUnicodeCategory(stFormD[ich]);

        if (uc != System.Globalization.UnicodeCategory.NonSpacingMark)
        {
            sb.Append(stFormD[ich]);
        } // End if (uc != System.Globalization.UnicodeCategory.NonSpacingMark)

    } // Next ich


    //return (sb.ToString().Normalize(System.Text.NormalizationForm.FormC));
    return (sb.ToString().Normalize(System.Text.NormalizationForm.FormKC));
} // End Function Latinize

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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