如何识别字符串是否包含 unicode 字符? [英] How to recognize if a string contains unicode chars?

查看:33
本文介绍了如何识别字符串是否包含 unicode 字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,我想知道它里面是否有 unicode 字符.(如果它完全包含 ASCII 或不)

I have a string and I want to know if it has unicode characters inside or not. (if its fully contains ASCII or not)

我怎样才能做到这一点?

How can I achieve that?

谢谢!

推荐答案

如果我的假设是正确的,您希望知道您的字符串是否包含任何非 ANSI"字符.您可以按如下方式推导.

If my assumptions are correct you wish to know if your string contains any "non-ANSI" characters. You can derive this as follows.

    public void test()
    {
        const string WithUnicodeCharacter = "a hebrew character:uFB2F";
        const string WithoutUnicodeCharacter = "an ANSI character:Æ";

        bool hasUnicode;

        //true
        hasUnicode = ContainsUnicodeCharacter(WithUnicodeCharacter);
        Console.WriteLine(hasUnicode);

        //false
        hasUnicode = ContainsUnicodeCharacter(WithoutUnicodeCharacter);
        Console.WriteLine(hasUnicode);
    }

    public bool ContainsUnicodeCharacter(string input)
    {
        const int MaxAnsiCode = 255;

        return input.Any(c => c > MaxAnsiCode);
    }

更新

这将检测扩展 ASCII.如果您只检测真正的 ASCII 字符范围(最多 127 个),那么您可能会误报不表示 Unicode 的扩展 ASCII 字符.我在我的示例中提到了这一点.

This will detect for extended ASCII. If you only detect for the true ASCII character range (up to 127), then you could potentially get false positives for extended ASCII characters which does not denote Unicode. I have alluded to this in my sample.

这篇关于如何识别字符串是否包含 unicode 字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆