在表示数字的 Unicode 字符中有几个范围,char.IsDigit
返回 true
。例如:
bool b1 = char.IsDigit('\uFF12'); // full-width '2' -> true
bool b2 = char.IsDigit('\u0665'); // true
bool b3 = char.IsDigit('5'); // true
我正在寻找一种方法来获取此类字符的数字等价物。请注意,int.Parse(...)
不起作用,因为它期望输入字符在基本 unicode 范围内('0' .. '9')。
这等同于 Java 的 Character.digit(...)
行为。
由于 .NET 框架的 char.IsDigit
方法正确地将此类字符识别为数字,我希望它也具有此功能,但我找不到任何东西。
最佳答案
你试过了吗Char.GetNumericValue
? (我只是启动我的 Windows 笔记本电脑来检查 :)
编辑:刚刚尝试过 - 看起来有效:
Console.WriteLine(char.GetNumericValue('\uFF12')); // 2
Console.WriteLine(char.GetNumericValue('\u0665')); // 5
Console.WriteLine(char.GetNumericValue('5')); // 5
请注意,这不仅包含数字 - 它是任何数字字符。但是,IsDigit
仅适用于数字。例如:
// U+00BD is the Unicode "vulgar fraction one half" character
Console.WriteLine(char.IsDigit('\u00bd')); // False
Console.WriteLine(char.GetNumericValue('\u00bd')); // 0.5
关于c# - 在 C# 中查找数字字符的数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12630007/