Detect Unicode Text in C#

Discussion in 'C#' started by pukhtoogle, Jan 11, 2014.

  1. #1
    I have a string, how can i detect whether it is Unicode or not. Basically my string is like

    پاکستان is our country
    PHP:
    If i double click on پاکستان the response should be Unicode for rest English text it shouldn't be Unicode text.
     
    pukhtoogle, Jan 11, 2014 IP
  2. Johnnt Nguyen

    Johnnt Nguyen Member

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #2
    Johnnt Nguyen, Jan 22, 2014 IP
  3. O-D-T

    O-D-T Member

    Messages:
    180
    Likes Received:
    10
    Best Answers:
    3
    Trophy Points:
    43
    #3
    Although your terminology is not exactly correct, if I understand your question correctly, you want to detect parts of a string that can be written in certain encoding (ASCII or maybe Latin-1) and parts that can not be written in certain encoding.

    If this is what you want then try to convert your text to the particular encoding - say ASCII and then compare it back with your string.

        private static bool IsAscii(string Part)
        {
          byte[] asciiBytes = Encoding.ASCII.GetBytes(Part);
          string partAscii = Encoding.ASCII.GetString(asciiBytes);
          return Part == partAscii;
        }
    
    
          string part = "test 1";
          Console.WriteLine("{0} {1} ASCII", part, IsAscii(part) ? "is" : "isn't");
    
          part = "čšýáířřží";
          Console.WriteLine("{0} {1} ASCII", part, IsAscii(part) ? "is" : "isn't");
    
          part = "پاکستان";
          Console.WriteLine("{0} {1} ASCII", part, IsAscii(part) ? "is" : "isn't");
    
          part = "is our country";
          Console.WriteLine("{0} {1} ASCII", part, IsAscii(part) ? "is" : "isn't");
    
    Code (markup):

    Now, for the first string and the last string, in the example above, the IsAscii function returns true. For the second and third, it returns false.
     
    O-D-T, Jun 9, 2014 IP