C#:有类似于 C# 的 php ctype_digit 函数吗?
我有一个字符串,我想检查它是否仅由数字组成。我真的不想(或需要)解析它或任何东西,我只是想知道它除了数字之外不包含任何内容(也没有浮点分隔符,只是数字)。
PHP 有一个很好的函数,名为 ctype_digit
。 .Net Framework 中是否有任何与 C# 类似的东西?或者我需要使用正则表达式或类似的东西吗?
为此创建一个正则表达式当然非常简单,但如果有更好的方法,我不想使用它:p
I have a string and I want to check that it only consists of digits. I don't really want to (or need to) parse it or anything, I just want to know that it does not contain anything but digits (and no floating point separator either, just digits).
PHP has this nice function called ctype_digit
. Is there anything similar for C# anywhere in the .Net Framework? Or do I need to use a regular expression or something like that?
Creating a regex for this would of course be pretty simple, but I would like to not use it if there are better ways :p
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用 int.TryParse。如果提供的字符串表示有效数字,它将返回 true,否则返回 false。不要忘记使用
NumerStyles.None
禁止空格和加号/减号。更新:正如 empi 所说,这不适用于非常大的字符串。如果您有任意大的字符串,也许正则表达式是您唯一的选择。你可以这样做
Regex.IsMatch(theString,"^[0-9]+$")
You could use int.TryParse. It will return true if the supplied string represents a valid number, false otherwise. Don't forget to use
NumerStyles.None
to disallow blank spaces and the plus/minus sign.UPDATE: As empi says, this will not work for very large strings. If you have arbitrarily large strings, maybe a regex is your only option. You could do something like
Regex.IsMatch(theString,"^[0-9]+$")
@布鲁诺·康德的答案 让我想到了这一点:D
不敢相信我之前没有想到......
The answer of @bruno conde made me think of this :D
Can't believe I didn't think of it before...
我认为,你最好使用正则表达式来为你完成这项工作......“\d+”应该做到......好吧,你说你不想使用它......但这是确保万无一失的方法字符串只包含数字,那么如果正则表达式通过,那么您可以直接使用 int.Parse(...) 。
Methinks, you'd be better to use regex to do the job for you... "\d+" should do it...ok, you said you do not want to use it... but it is foolproof way of ensuring the string contains only numbers, then if the regexp passes, then you can use int.Parse(...) straightaway.
是的,您可以使用
代码示例:
其他选项是使用正则表达式:
您可以根据您的要求更改模式。
Yes, you can use
Code sample:
Other option is to use Regex:
You can change the pattern as per your requirement.
没有内置方法。 Int32.TryParse 或 Int64.TryParse 不适用于仅包含数字的非常长的字符串(它们也可能允许使用其他字符来表示整数)。
There is no built-in method. Int32.TryParse or Int64.TryParse won't work for very long strings containing just numbers (they may also allow other chars used to represent integer numbers).
正如其他人所说,您可以使用
int.TryParse
方法,尽管除非您的数字真的非常大,否则您可以使用其他类型的TryParse
方法,即其范围比 int32 更大。 (当然使用NumberStyles.None
选项,以避免符号和标点符号)。细分如下:
int
-2,147,483,648 .. 2,147,483,647uint
0 .. 4,294,967,295long
-9,223,372,036,854,775,808 .. 9,223,372,036, 854,775,807乌龙
0 .. 18,446,744,073,709,551,615浮点
-3.402823e38 .. 3.402823e38双
-1.79769313486232e308 .. 1.79769313486232e308一个可以解析最大数字的是
Double
。如果您需要使用 number ,您将失去一些精度,但它可以解析非常长的数字(尽管您说您不需要使用它,所以它不应该成为问题)。在我进行的一项快速测试中,它成功解析了以下字符串:(即 308 个字符,多一个数字就会失败)
不过,如果您不打算使用该数字,它可能会有点过大,所以我会使用正则表达式,或者更好的是循环检查每个字符是否是数字。
如果你想变得有点疯狂,你可以将其分成几个较小的字符串,并使用任务库以并行方式检查它:P
(我知道现在有点离题,但如果你确实想这样做,你应检查
parallel.for
和Partition Ranger
,查看此 C9 10 分钟剪辑:Parallel For Partition Ranger )As other people have said, you can use the
int.TryParse
method, although unless your number is really really EXTREMELY big, you could use theTryParse
method of other types, that have a bigger range than int32's. (of course using theNumberStyles.None
option, to avoid signs and punctuation).Here's the break down:
int
-2,147,483,648 .. 2,147,483,647uint
0 .. 4,294,967,295long
-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807ulong
0 .. 18,446,744,073,709,551,615float
-3.402823e38 .. 3.402823e38double
-1.79769313486232e308 .. 1.79769313486232e308decimal
-79228162514264337593543950335 .. 79228162514264337593543950335The one that can parse the biggest numbers is
Double
. If you need to use the number , you will lose some precision, but it can parse really long numbers (although you say you dont need to use it so it shouldnt be a problem). In a quick test I did, it managed to successfully parse the following string:(thats 308 characters, it would fail with one more number)
Still, if you are not going to use the number, it might be an overkill, so I would go for the Regex, or even better the loop checking that each character is a digit.
If you then wanna go a little crazy you could split that into several smaller strings, and use the Task library to check it in a parallel way :P
(I know its a little offtopic now, but if you DO want to do that, you should check
parallel.for
and thePartition Ranger
, check out this C9 10min clip: Parallel For Partition Ranger )