s[0].isdigit_国际化/ I18n:Char.IsDigit()不仅可以匹配“ 0”到“ 9”

s[0].isdigit

s[0].isdigit

Raymond Chen just gave me a "duh" moment, by pointing out the obvious-only-if-you-think-about-it.  Char.IsDigit() doesn't mean 'IsZeroToNineInEnglish', it means 'is in the decimal range of 0 to 9' and darnnit if there aren't other ways (other than 0,1,2,3,4,5,6,7,8,9) to express them! :)

雷蒙德·陈(Raymond Chen)指出了显而易见的“仅当您考虑”时给了我一个“傻”的时刻。 Char.IsDigit()的意思不是'IsZeroToNineInEnglish',它的意思是'位于0到9的十进制范围内',如果没有其他方式(除了0、1、2、3、4、5, 6,7,8,9)表达他们! :)

So let's run an experiment.

class Program {
  public static void Main(string[] args) {
    System.Console.WriteLine(
      System.Text.RegularExpressions.Regex.Match(
        "\x0661\x0662\x0663", // "١٢٣"
        "^\\d+$").Success);
    System.Console.WriteLine(
      System.Char.IsDigit('\x0661'));
  }
}

The characters in the string are Arabic digits, but they are still digits, as evidenced by the program output:

True
True
Uh-oh. Do you have this bug in your parameter validation? ( More examples. .)
If you use a pattern like @"^\d$" to validate that you receive only digits, and then later use System.Int32.Parse() to parse it, then I can hand you some Arabic digits and sit back and watch the fireworks. The Arabic digits will pass your validation expression, but when you get around to using it, boom, you throw a System.FormatException and die.
[ Raymond Chen ]

因此,让我们进行实验。

 class Program {
  public static void Main(string[] args) {
    System.Console.WriteLine(
      System.Text.RegularExpressions.Regex.Match(
        "\x0661\x0662\x0663", // " ١٢٣ "
        "^\\d+$").Success);
    System.Console.WriteLine(
      System.Char.IsDigit('\x0661'));
  }
}

字符串中的字符是阿拉伯数字,但仍然是数字,如程序输出所示:

哦哦 参数验证中是否存在此错误? ( 更多示例 。) 如果您使用@"^\d$"来验证您仅接收到数字,然后再使用System.Int32.Parse()进行解析,那么我可以为您提供一些阿拉伯数字,然后坐下来观看烟花。 阿拉伯数字将通过您的验证表达式,但是当您开始使用该表达式时,会抛出System.FormatException并死亡。 [ 陈百强 ]

Arabic speakers (مرحبًا, كيف حالك ؟ and forgive me, it's been college since I studied Arabic) how to you handle numeric validation in JavaScript AND guarantee that the JavaScript you use on the client-side is semantically equivalent to the server-side code?

讲阿拉伯语(مرحبا,كيفحالك?原谅我来说,这是大学,因为我学的阿拉伯语)怎么给处理JavaScript数字验证保证您在客户端使用JavaScript在语义上等同于服务器端代码?

Either way, my friends, read, grok, and be enlightened.  Muy interesante.

无论哪种方式,我的朋友们都可以阅读,阅读和启发。 Muy interesante。

翻译自: https://www.hanselman.com/blog/internationalizationi18n-charisdigit-matches-more-than-just-0-through-9

s[0].isdigit

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值