C#:有类似于 C# 的 php ctype_digit 函数吗?

发布于 2024-08-11 15:56:16 字数 336 浏览 12 评论 0原文

我有一个字符串,我想检查它是否仅由数字组成。我真的不想(或需要)解析它或任何东西,我只是想知道它除了数字之外不包含任何内容(也没有浮点分隔符,只是数字)。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

维持三分热 2024-08-18 15:56:16

您可以使用 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]+$")

梦里寻她 2024-08-18 15:56:16

@布鲁诺·康德的答案 让我想到了这一点:D

if(subject.All(char.IsDigit))
    // Do something

不敢相信我之前没有想到......

The answer of @bruno conde made me think of this :D

if(subject.All(char.IsDigit))
    // Do something

Can't believe I didn't think of it before...

羁客 2024-08-18 15:56:16
bool onlyDigits = "1234".All(c => char.IsDigit(c));
bool onlyDigits = "1234".All(c => char.IsDigit(c));
時窥 2024-08-18 15:56:16

我认为,你最好使用正则表达式来为你完成这项工作......“\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.

戴着白色围巾的女孩 2024-08-18 15:56:16

是的,您可以使用

bool Int32.TryParse(string s, out int result);

代码示例:

string myString = "1265";
int myInt;

if (Int32.TryParse(myString,myInt) // Returns true if mystring contains only digits
{
...
}

其他选项是使用正则表达式:

    public static bool IsDigit(string myString)
    {
        string pattern = @"^\d*$";

        if (Regex.IsMatch(myString, pattern))
            return true;
        else
            return false;
    }

您可以根据您的要求更改模式。

Yes, you can use

bool Int32.TryParse(string s, out int result);

Code sample:

string myString = "1265";
int myInt;

if (Int32.TryParse(myString,myInt) // Returns true if mystring contains only digits
{
...
}

Other option is to use Regex:

    public static bool IsDigit(string myString)
    {
        string pattern = @"^\d*$";

        if (Regex.IsMatch(myString, pattern))
            return true;
        else
            return false;
    }

You can change the pattern as per your requirement.

薄荷梦 2024-08-18 15:56:16

没有内置方法。 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).

待"谢繁草 2024-08-18 15:56:16

正如其他人所说,您可以使用 int.TryParse 方法,尽管除非您的数字真的非常大,否则您可以使用其他类型的 TryParse 方法,即其范围比 int32 更大。 (当然使用 NumberStyles.None 选项,以避免符号和标点符号)。

细分如下:

  1. int -2,147,483,648 .. 2,147,483,647
  2. uint 0 .. 4,294,967,295
  3. long -9,223,372,036,854,775,808 .. 9,223,372,036, 854,775,807
  4. 乌龙 0 .. 18,446,744,073,709,551,615
  5. 浮点 -3.402823e38 .. 3.402823e38
  6. -1.79769313486232e308 .. 1.79769313486232e308
  7. >十进制 -79228162514264337593543950335 .. 79228162514264337593543950335

一个可以解析最大数字的是Double。如果您需要使用 number ,您将失去一些精度,但它可以解析非常长的数字(尽管您说您不需要使用它,所以它不应该成为问题)。在我进行的一项快速测试中,它成功解析了以下字符串:(

79228162514264337593543950335792281625142643375935439503357922816251426433759354395033579228162514264337593543950335792281625142643375935439503357922816251426433759354395033579228162514264337593543950335792281625142643375935439503357922816251426433759354395033579228162514264337593543950335234234234234243423

即 308 个字符,多一个数字就会失败)

不过,如果您不打算使用该数字,它可能会有点过大,所以我会使用正则表达式,或者更好的是循环检查每个字符是否是数字。

如果你想变得有点疯狂,你可以将其分成几个较小的字符串,并使用任务库以并行方式检查它:P

(我知道现在有点离题,但如果你确实想这样做,你应检查 parallel.forPartition 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 the TryParse method of other types, that have a bigger range than int32's. (of course using the NumberStyles.None option, to avoid signs and punctuation).

Here's the break down:

  1. int -2,147,483,648 .. 2,147,483,647
  2. uint 0 .. 4,294,967,295
  3. long -9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807
  4. ulong 0 .. 18,446,744,073,709,551,615
  5. float -3.402823e38 .. 3.402823e38
  6. double -1.79769313486232e308 .. 1.79769313486232e308
  7. decimal -79228162514264337593543950335 .. 79228162514264337593543950335

The 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:

79228162514264337593543950335792281625142643375935439503357922816251426433759354395033579228162514264337593543950335792281625142643375935439503357922816251426433759354395033579228162514264337593543950335792281625142643375935439503357922816251426433759354395033579228162514264337593543950335234234234234243423

(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 the Partition Ranger, check out this C9 10min clip: Parallel For Partition Ranger )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文