简体   繁体   English

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

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

I just want to know, whether a String variable contains a parsable positive 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.注意:我知道扩展方法,但我想知道是否有内置的东西。

You could use char.IsDigit :你可以使用char.IsDigit

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

Will return true if the string is a number如果字符串是数字,则返回true

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

Will return true if the string contains a digit如果字符串包含数字,则返回true

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

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

Maybe this can help也许这可以帮助

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

answer from msdn .来自msdn 的回答。

You can check if string contains numbers only:您可以检查字符串是否仅包含数字:

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

But number can be bigger than Int32.MaxValue or less than Int32.MinValue - you should keep that in mind.但是 number 可以大于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())
    // ...

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));

这对我有用。

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

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

        string text = Console.ReadLine();
        bool isNumber = false;

        for (int i = 0; i < text.Length; i++)
        {
            if (char.IsDigit(text[i]))
            {
                isNumber = true;
                break;
            }
        }

        if (isNumber)
        {
            Console.WriteLine("Text contains number.");
        }
        else
        {
            Console.WriteLine("Text doesn't contain number.");
        }

        Console.ReadKey();

Or Linq:或林克:

        string text = Console.ReadLine();

        bool isNumberOccurance =text.Any(letter => char.IsDigit(letter));
        Console.WriteLine("{0}",isDigitPresent ? "Text contains number." : "Text doesn't contain number.");
        Console.ReadKey();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM