久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么 Char.IsDigit 對于無法解析為 int 的字符返回

Why Char.IsDigit returns true for chars which can#39;t be parsed to int?(為什么 Char.IsDigit 對于無法解析為 int 的字符返回 true?)
本文介紹了為什么 Char.IsDigit 對于無法解析為 int 的字符返回 true?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習吧!

問題描述

我經(jīng)常使用字符.IsDigit 來檢查 char 是否是一個數(shù)字,這在 LINQ 查詢中特別方便以預(yù)先檢查 int.Parse 如下:"123".All(Char.IsDigit).

但是有些字符是數(shù)字,但不能像 ? 那樣解析為 int.

//真bool isDigit = Char.IsDigit('?');var文化 = CultureInfo.GetCultures(CultureTypes.SpecificCultures);整數(shù);//錯誤的bool isIntForAnyCulture = 文化.Any(c => int.TryParse('?'.ToString(), NumberStyles.Any, c, out num));

這是為什么?我的 int.Parse-通過 Char.IsDigit 進行預(yù)檢查是否不正確?

有 310 個字符是數(shù)字:

ListdigitList = Enumerable.Range(0, UInt16.MaxValue).Select(i => Convert.ToChar(i)).Where(c => Char.IsDigit(c)).ToList();

以下是 .NET 4 (ILSpy) 中 Char.IsDigit 的實現(xiàn):

public static bool IsDigit(char c){如果 (char.IsLatin1(c)){返回 c >= '0' &&c <= '9';}返回 CharUnicodeInfo.GetUnicodeCategory(c) == UnicodeCategory.DecimalDigitNumber;}

那么為什么會有屬于 DecimalDigitNumber-category("十進制數(shù)字字符,即 0 到 9 范圍內(nèi)的字符...")在任何文化中都不會被解析為 int 嗎?

解決方案

這是因為它正在檢查 Unicode數(shù)字,十進制數(shù)字"類別中的所有數(shù)字,如下所列:

http://www.fileformat.info/info/unicode/類別/Nd/list.htm

這并不意味著它是當前語言環(huán)境中的有效數(shù)字字符.事實上,使用int.Parse(),你只能解析正常的英文數(shù)字,??而不管區(qū)域設(shè)置如何.

例如,這不起作用:

int test = int.Parse("?", CultureInfo.GetCultureInfo("ar"));

即使 ? 是有效的阿拉伯數(shù)字字符,并且ar"是阿拉伯語區(qū)域設(shè)置標識符.

Microsoft 文章 如何:解析 Unicode 數(shù)字" 指出那個:

<塊引用><塊引用>

.NET Framework 解析為十進制的唯一 Unicode 數(shù)字是 ASCII 數(shù)字 0 到 9,由代碼值 U+0030 到 U+0039 指定..NET Framework 將所有其他 Unicode 數(shù)字解析為字符.

但是,請注意,您可以使用 char.GetNumericValue() 將 unicode 數(shù)字字符轉(zhuǎn)換為雙精度數(shù)字.

返回值是 double 而不是 int 的原因是這樣的:

Console.WriteLine(char.GetNumericValue('?'));//打印 0.25

您可以使用類似的方法將字符串中的所有數(shù)字字符轉(zhuǎn)換為它們的 ASCII 等價物:

public string ConvertNumericChars(string input){StringBuilder 輸出 = new StringBuilder();foreach(輸入中的字符ch){如果 (char.IsDigit(ch)){雙值 = char.GetNumericValue(ch);if ((value >= 0) && (value <= 9) && (value == (int)value)){output.Append((char)('0'+(int)value));繼續(xù);}}output.Append(ch);}返回 output.ToString();}

I often use Char.IsDigit to check if a char is a digit which is especially handy in LINQ queries to pre-check int.Parse as here: "123".All(Char.IsDigit).

But there are chars which are digits but which can't be parsed to int like ?.

// true
bool isDigit = Char.IsDigit('?'); 

var cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
int num;
// false
bool isIntForAnyCulture = cultures
    .Any(c => int.TryParse('?'.ToString(), NumberStyles.Any, c, out num)); 

Why is that? Is my int.Parse-precheck via Char.IsDigit thus incorrect?

There are 310 chars which are digits:

List<char> digitList = Enumerable.Range(0, UInt16.MaxValue)
   .Select(i => Convert.ToChar(i))
   .Where(c => Char.IsDigit(c))
   .ToList(); 

Here's the implementation of Char.IsDigit in .NET 4 (ILSpy):

public static bool IsDigit(char c)
{
    if (char.IsLatin1(c))
    {
        return c >= '0' && c <= '9';
    }
    return CharUnicodeInfo.GetUnicodeCategory(c) == UnicodeCategory.DecimalDigitNumber;
}

So why are there chars that belong to the DecimalDigitNumber-category("Decimal digit character, that is, a character in the range 0 through 9...") which can't be parsed to an int in any culture?

解決方案

It's because it is checking for all digits in the Unicode "Number, Decimal Digit" category, as listed here:

http://www.fileformat.info/info/unicode/category/Nd/list.htm

It doesn't mean that it is a valid numeric character in the current locale. In fact using int.Parse(), you can ONLY parse the normal English digits, regardless of the locale setting.

For example, this doesn't work:

int test = int.Parse("?", CultureInfo.GetCultureInfo("ar"));

Even though ? is a valid Arabic digit character, and "ar" is the Arabic locale identifier.

The Microsoft article "How to: Parse Unicode Digits" states that:

The only Unicode digits that the .NET Framework parses as decimals are the ASCII digits 0 through 9, specified by the code values U+0030 through U+0039. The .NET Framework parses all other Unicode digits as characters.

However, note that you can use char.GetNumericValue() to convert a unicode numeric character to its numeric equivalent as a double.

The reason the return value is a double and not an int is because of things like this:

Console.WriteLine(char.GetNumericValue('?')); // Prints 0.25

You could use something like this to convert all numeric characters in a string into their ASCII equivalent:

public string ConvertNumericChars(string input)
{
    StringBuilder output = new StringBuilder();

    foreach (char ch in input)
    {
        if (char.IsDigit(ch))
        {
            double value = char.GetNumericValue(ch);

            if ((value >= 0) && (value <= 9) && (value == (int)value))
            {
                output.Append((char)('0'+(int)value));
                continue;
            }
        }

        output.Append(ch);
    }

    return output.ToString();
}

這篇關(guān)于為什么 Char.IsDigit 對于無法解析為 int 的字符返回 true?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Is there a way to know if someone has bookmarked your website?(有沒有辦法知道是否有人為您的網(wǎng)站添加了書簽?)
Use of Different .Net Languages?(使用不同的 .Net 語言?)
Is there a C# library that will perform the Excel NORMINV function?(是否有執(zhí)行 Excel NORMINV 函數(shù)的 C# 庫?)
Determining an #39;active#39; user count of an ASP.NET site(確定 ASP.NET 站點的“活動用戶數(shù))
Select x random elements from a weighted list in C# (without replacement)(從 C# 中的加權(quán)列表中選擇 x 個隨機元素(無需替換))
Best way to keep track of current online users(跟蹤當前在線用戶的最佳方式)
主站蜘蛛池模板: 欧区一欧区二欧区三免费 | 欧美综合一区二区 | 日本不卡一区二区 | 日韩成人免费视频 | 日韩欧美在线视频 | 中文字幕精品视频 | 久久成人精品视频 | 久久综合久久综合久久综合 | 久草在线中文888 | 日韩欧美二区 | 亚洲成人av在线播放 | 久久99精品久久久久婷婷 | 欧美精品网站 | 亚洲成人午夜电影 | 国产小u女发育末成年 | 亚洲国产网址 | 国产一区黄色 | 国产精品久久久久久久久久久久久 | 亚洲欧美一区二区三区视频 | 五月天婷婷丁香 | 欧美福利视频 | 欧美日韩午夜精品 | 找个黄色片 | 成人欧美一区二区三区白人 | 在线播放精品视频 | 国产乱码精品一区二区三区忘忧草 | 国产激情免费视频 | 欧美一区二区三区在线 | 嫩草视频在线免费观看 | 亚洲精品不卡 | 日韩中文字幕 | 在线亚洲精品 | 国产999精品久久久久久 | 欧美国产日韩一区二区三区 | 成人欧美一区二区三区黑人孕妇 | 免费精品视频在线观看 | 一级黄色毛片a | 日韩高清国产一区在线 | 久久99精品久久久久久国产越南 | 天天天天操 | 一级在线观看 |