Himberjack Himberjack - 1 year ago 85
ASP.NET (C#) Question

How to recognize if a string contains unicode chars?

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?


Answer Source

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;

        hasUnicode = ContainsUnicodeCharacter(WithUnicodeCharacter);

        hasUnicode = ContainsUnicodeCharacter(WithoutUnicodeCharacter);

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

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


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download