In C #, why do two functions have different processing methods for full-width and half-width numeric characters?

Source: Internet
Author: User

Background

During development, you need to determine whether an input character is a numeric character. If it is a numeric character, it is converted to an integer.

Problem proposal

The char. IsDigit method and int. TryParse method are used during implementation. It is found that char. isDigit determines it as a numeric character and uses int. the TryParse method does not recognize it as a numeric character. Why?

Problem Reproduction

Instance code

using System;namespace QuanJiaoShuZiExp{    class Program    {        static void Main(string[] args)        {            string str1 = "4";            string str2 = "4";            Console.WriteLine(char.IsDigit(str1, 0));            Console.WriteLine(char.IsDigit(str2, 0));             int result;            Console.WriteLine(int.TryParse(str1, out result));            Console.WriteLine(int.TryParse(str2, out result));        }    }}
Code execution result

The result shows that char. isDigit method and int. returns true for all TryParse methods. When a full-width numeric character is input, char. the IsDigit method returns true, whereas int. the TryParse method returns false.

Problem Discussion

Why?

To answer this question, you may have to ask Microsoft, because these two methods are implemented by Microsoft.

No matter what Microsoft thinks, I will talk about this issue from my own perspective.

First, we know that the halfwidth is the standard usage of international input methods. In order to adapt to Chinese Input habits, the Chinese Input Method adds the fullwidth. Secondly, we know that the halfwidth is ASCII characters, and the fullwidth refers to various symbols in the GB2312-80 ("information exchange with Chinese character encoding Character Set-basic set. Since the full-width characters and the half-width characters come from different character sets, the encoding of the full-width numeric characters and the half-width numeric characters may be different (I am not sure about this, because do not know much about GB2312-80 encoding, please kindly enlighten me), So I boldly guess, char. the IsDigit method may convert the input character encoding to make the full-width numeric character equivalent to the half-width numeric character, while the int. the TryParse method does not exist, so the full-width half-width characters are treated separately.

Let's talk about it today. I feel like I still want to talk about it. I don't want to write it too much. I will try again later.

I want to raise a question here. I hope you can give me some advice.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.