none
char.IsLetter 中文判断为是字母 RRS feed

  • 问题

  • using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Data.Common;
    using System.Security.Cryptography;
    
    namespace main
    {
        public class Entry1
        {
            public static void Main()
            {
                string str = "a1中2A";
                for (int i = 0; i < str.Length; i++)
                {
                    if (char.IsLetter(str[i])) Console.WriteLine("字母 " + str[i]);
                    else if (char.IsDigit(str[i])) Console.WriteLine("数字 " + str[i]);
                }
            }
        }
    }

    输出为:

    字母 a
    数字 1
    字母 中
    数字 2
    字母 A

    为什么会这样呢?

    2015年6月10日 2:36

答案

  • Hello,

    >>为什么会这样呢?

    IsLetter是根据Unicode来判断是否为letter:

    https://msdn.microsoft.com/en-us/library/system.char.isletter(v=vs.110).aspx:

    This method determines whether a Char is a member of any category of Unicode letter. Unicode letters include the following:

    你可以看到对于这个字符,它的Unicode码是: U+4E2D 属于otherletter的范围之内,是一个字母。

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    2015年6月10日 7:17
    版主

全部回复

  • Hello,

    >>为什么会这样呢?

    IsLetter是根据Unicode来判断是否为letter:

    https://msdn.microsoft.com/en-us/library/system.char.isletter(v=vs.110).aspx:

    This method determines whether a Char is a member of any category of Unicode letter. Unicode letters include the following:

    你可以看到对于这个字符,它的Unicode码是: U+4E2D 属于otherletter的范围之内,是一个字母。

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    2015年6月10日 7:17
    版主
  • 原来是这样啊,可是对于中国来说,汉字不是字母啊,为什么会把汉字划分在字母类呢?
    2015年6月12日 13:47
  • 哦,这下知道了,谢谢^_^
    2015年6月16日 13:42