是否有 Int.isWholeNumber() 函数或类似的函数?
我需要检查输入是否为 int。是否有与 String.IsNullOrEmpty() 类似的函数,例如 Int.isWholeNumber() 函数?
有没有办法仅在 if()
语句中验证这一点,而不必事先声明 int ? (正如您需要使用 TryParse() 所做的那样)
编辑
我需要验证区号(五个数字)
I need to check if an input is an int or not. Is there a similar function to the String.IsNullOrEmpty(), like an Int.isWholeNumber() function?
Is there a way to validate this inside the if()
statement only, without having to declare an int before? (As you need to do with TryParse())
EDIT
I need to validate an area code (five numbers)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我不相信 BCL 中有任何这样的方法,但编写一个很容易(我假设您真的在谈论字符串是否可以解析为整数):
添加接受
IFormatProvider值等,根据您的需要。假设这是为了验证,您可能希望扩展它以允许指定一系列有效值...
如果您正在进行大量自定义验证,您可能希望查看 Fluent Validation 项目。
I don't believe there's any such method within the BCL, but it's easy to write one (I'm assuming you're really talking about whether a string can be parsed as an integer):
Add overloads accepting
IFormatProvider
values etc as you require them. Assuming this is for validation, you might want to expand it to allow a range of valid values to be specified as well...If you're doing a lot of custom validation, you may well want to look at the Fluent Validation project.
我从您的评论中了解到,您的实际问题是“如何确定该字符串是否包含有效的瑞典邮政编码?”瑞典邮政编码是一个不以零开头的五位数字。如果这是您实际需要解决的问题,那么解决该问题。我不会尝试将字符串转换为整数然后检查整数,而是简单地编写检查:
就这么简单。如果您永远不会对其进行数学运算,那么首先不要将其转换为整数。
这种方法将进一步推广到更复杂的形式。据我所知,瑞典邮政编码通常以“SE-12 345”的形式书写,即带有前缀“SE-”,数字二和三之间有一个空格。编写处理该格式的整数验证例程将非常困难,但编写字符串验证例程却很简单。
更一般地说,这说明了一些关于编写问题的好建议。 提出有关您实际必须解决的问题的问题。您假设了一个解决方案——将字符串解析为整数——然后开始询问有关您假设的解决方案的问题。这会自动阻止任何人针对您的实际问题提供建议。也许阅读本文的人已经开发了一个邮政编码验证软件库;如果他们有,他们永远不会知道从你最初的问题中告诉你这一点。
I gather from your comments that your actual problem is "how do I determine if this string contains a valid Swedish postal code?" A Swedish postal code is a five digit number not beginning with a zero. If that's the problem you actually have to solve, then solve that problem. Rather than trying to convert the string to an integer and then check the integer, I would simply write checks that say:
Simple as that. If you're never going to do math on it, don't convert it to an integer in the first place.
This approach will further generalize to more complex forms. Swedish postal codes, I gather, are often written in the form "SE-12 345", that is, with the prefix "SE-" and a space between digits two and three. It's going to be awfully hard to write an integer-validating routine that deals with that format, but writing a string-validating routine is straightforward.
More generally, this illustrates some good advice for writing questions. Ask a question about the problem you actually must solve. You assumed a solution -- parse the string as an integer -- and then started asking questions about your assumed solution. That automatically precludes anyone from giving advice that is specific to your real problem. Maybe someone reading this has already developed a library of postal-code validating software; if they have, they'd never know to tell you about it from your original question.
您可以通过扩展方法来实现这一点:
您使用
以下代码:
You can achieve this by extension methods:
You use
Here is code:
查看 int.TryParse 是否为您提供答案,并确保输入字符串中没有小数点。 (如果有人输入“1.”作为输入,这会给您带来误报。)
See if int.TryParse gives you an answer, and make sure there's not a decimal point in the input string. (This will give you a false positive if someone enters "1." as the input.)
使用
Int.TryParse
或者制作自定义函数。Use
Int.TryParse
Or make a custom function.根据定义,int 类型必须是整数。它必须是 double、float 或decimal 类型才能成为非整数。您始终可以尝试测试输入的类型。
或者也许输入是一个字符串?如果输入是字符串,您可以尝试搜索“.”或“,”字符(取决于文化)。或者您可以尝试解析为整数。
int types have to be whole numbers by definition. It would have to be type double, float, or decimal to be a non-whole number. You can always try testing the type of the input.
Or maybe the input is a string? If the input is a string, you could try searching for a '.' or ',' character (depending on the culture). Or you could try parsing as an integer.
由于每个 int 都是整数,因此这很简单:
Since every int is a whole number, this is easy:
如果您只想在 if 语句中验证它,您可以这样做
If you just want to validate it in the if statement you could just do