![s[0].isdigit](https://i-blog.csdnimg.cn/blog_migrate/0b1b56ba584f33002aa57c7b97bb173d.png)
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:
Uh-oh. Do you have this bug in your parameter validation? ( More examples. .)True True
If you use a pattern like@"^\d$"
to validate that you receive only digits, and then later useSystem.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 aSystem.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。
s[0].isdigit