You must Sign In to post a response.
  • Category: ASP.NET

    How to find out that string contain unicode character in C#

    I came across situation where i want to check that given string contain unicode character or not?How to do that?
  • #766594
    Hi,

    If you want to check whether the string contains UniCode character or not, for that you have to check Character value and compare with 127, if it is greater than 127 it is not ASCII.


    string input="AÀBßCC???";
    foreach (char c in input)
    {
    if (c > 127)
    {
    //string doesn't contains UniCode characters
    }
    else
    {
    //string contains UniCode characters
    }
    }


    Hope this will helpful to you....

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #766599
    Thanks for ur reply.But I think should compare with 255 not 127..string input = "Non English";

    if(input.Any(c => c > 255))
    {
    // unicode
    }

  • #766601
    Hi,

    AS per MSDN, ASCII only extends up to 127. 128..255 is commonly used for ANSI characters. Above that is only Unicode. If you have any questions please go through below link that might be helpful to you.
    https://social.msdn.microsoft.com/Forums/en-US/30c27587-82a8-4fb0-a94b-3739f9c8e53d/detecting-unicode-character?forum=csharplanguage

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #766606
    Thanks for ur reply.But I think should compare with 255 not 127..string input = "Non English";

    if(input.Any(c => c > 255))
    {
    // unicode
    }

  • #766632
    HI,

    Try this one..
    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)
    )
    );

    Other wise, You will check the below mentioned Links. It will provide some ideas for,
    1.http://stackoverflow.com/questions/4459571/how-to-recognize-if-a-string-contains-unicode-chars
    2.http://stackoverflow.com/questions/1522884/c-sharp-ensure-string-contains-only-ascii
    3.http://stackoverflow.com/questions/123336/how-can-you-strip-non-ascii-characters-from-a-string-in-c
    4.http://www.codeproject.com/Questions/1107914/How-to-find-out-that-string-contain-unicode-charac

  • #766642
    Hi,

    Refer below link and see if the sample code works as per your requirement,

    http://stackoverflow.com/questions/4459571/how-to-recognize-if-a-string-contains-unicode-chars


    Regards,
    Asheej T K
    Microsoft MVP[ASP.NET/IIS]
    DotNetSpider MVM


Sign In to post your comments