在 C# 中,如何检查字符串是否包含整数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18251875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:40:35  来源:igfitidea点击:

In C#, how to check whether a string contains an integer?

c#stringparsingintegertryparse

提问by Marcel

I just want to know, whether a String variable contains a parsablepositive integer value. I do NOT want to parse the value right now.

我只想知道,字符串变量是否包含可解析的正整数值。我现在不想解析该值。

Currently I am doing:

目前我正在做:

int parsedId;
if (
    (String.IsNullOrEmpty(myStringVariable) ||
    (!uint.TryParse(myStringVariable, out parsedId))
)
{//..show error message}

This is ugly - How to be more concise?

这很丑 - 如何更简洁?

Note: I know about extension methods, but I wonder if there is something built-in.

注意:我知道扩展方法,但我想知道是否有内置的东西。

采纳答案by Marcel

The answer seems to be just no.

答案似乎是否定的。

Although there are many good other answers, they either just hide the uglyness (which I did not ask for) or introduce new problems (edge cases).

尽管还有许多其他不错的答案,但它们要么只是隐藏了丑陋之处(我没有要求),要么引入了新问题(边缘情况)。

回答by dtb

Assuming you want to check that all characters in the string are digits, you could use the Enumerable.All Extension Methodwith the Char.IsDigit Methodas follows:

假设您想检查字符串中的所有字符是否都是数字,您可以使用Enumerable.All 扩展方法Char.IsDigit 方法,如下所示:

bool allCharactersInStringAreDigits = myStringVariable.All(char.IsDigit);

回答by SBoss

Sorry, didn't quite get your question. So something like this?

抱歉,没有完全明白你的问题。所以像这样的事情?

str.ToCharArray().Any(char.IsDigit);

Or does the value have to be an integer completely, without any additional strings?

还是该值必须完全是整数,而没有任何其他字符串?

if(str.ToCharArray().All(char.IsDigit(c));

回答by Sergey Berezovskiy

You can check if string contains numbers only:

您可以检查字符串是否仅包含数字:

Regex.IsMatch(myStringVariable, @"^-?\d+$")

But number can be bigger than Int32.MaxValueor less than Int32.MinValue- you should keep that in mind.

但是数字可以大于Int32.MaxValue或小于Int32.MinValue- 您应该记住这一点。

Another option - create extension method and move ugly code there:

另一种选择 - 创建扩展方法并在那里移动丑陋的代码:

public static bool IsInteger(this string s)
{
   if (String.IsNullOrEmpty(s))
       return false;

   int i;
   return Int32.TryParse(s, out i);
}

That will make your code more clean:

这将使您的代码更干净:

if (myStringVariable.IsInteger())
    // ...

回答by DGibbs

You could use char.IsDigit:

你可以使用char.IsDigit

     bool isIntString = "your string".All(char.IsDigit)

Will return trueif the string is a number

true如果字符串是数字,将返回

    bool containsInt = "your string".Any(char.IsDigit)

Will return trueif the string contains a digit

true如果字符串包含数字,则返回

回答by panako

Maybe this can help

也许这可以帮助

string input = "hello123world";
bool isDigitPresent = input.Any(c => char.IsDigit(c));

answer from msdn.

来自msdn 的回答。

回答by sarmad salik

This work for me.

这对我有用。

("your string goes here").All(char.IsDigit)