如何识别,如果一个字符串包含单code字符? [英] How to recognize if a string contains unicode chars?

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

问题描述

我有一个字符串,我想知道是否有内部与否单code字符。
(如果其完全包含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),那么你可能会得到误报这并不表示统一code扩展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.

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

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