我正在使用C#.如何验证输入字符串是否为整数且仅由数字0-9组成?
另外一个要求是它应该总是由9位数字组成; 不多也不少.
例如,
好的:118356737,1111111111,123423141,
错误:11a334356,1.2.4.535,1112234.222等
谢谢
Dou*_*las 10
你可以使用正则表达式:
string input = "123456789";
bool isValid = Regex.IsMatch(input, @"^\d{9}$");
Run Code Online (Sandbox Code Playgroud)
或LINQ:
string input = "123456789";
bool isValid = input.Length == 9 && input.All(char.IsDigit);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19670 次 |
| 最近记录: |