C# 测试字符串是否为整数?

C# 测试字符串是否为整数?,c#,.net,string,integer,C#,.net,String,Integer,我只是好奇C#语言或.NET框架中是否内置了某种东西来测试某个东西是否是整数 if (x is an int) // Do something 在我看来可能有,但我只是一名编程专业的一年级学生,所以我不知道。使用这种方法 如果成功解析,它将返回true,输出结果的值将为整数。如果您只想检查传递变量的类型,您可能可以使用: var a = 2; if (a is int) { //is integer } //or: if (

我只是好奇C#语言或.NET框架中是否内置了某种东西来测试某个东西是否是整数

if (x is an int)
   // Do something
在我看来可能有,但我只是一名编程专业的一年级学生,所以我不知道。

使用这种方法


如果成功解析,它将返回true,输出结果的值将为整数。

如果您只想检查传递变量的类型,您可能可以使用:

    var a = 2;
    if (a is int)
    {
        //is integer
    }
    //or:
    if (a.GetType() == typeof(int))
    {
        //is integer
    }

我记得我看过int.TryParse和int.Parse Regex之间的性能比较,char.IsNumber和char.IsNumber是最快的。无论如何,不管表现如何,这里还有一种方法

        bool isNumeric = true;
        foreach (char c in "12345")
        {
            if (!Char.IsNumber(c))
            {
                isNumeric = false;
                break;
            }
        }

我已经编写了大约2周的代码,并创建了一个简单的逻辑来验证整数是否被接受

    Console.WriteLine("How many numbers do you want to enter?"); // request a number
    string input = Console.ReadLine(); // set the input as a string variable
    int numberTotal; // declare an int variable

    if (!int.TryParse(input, out numberTotal)) // process if input was an invalid number
    {
        while (numberTotal  < 1) // numberTotal is set to 0 by default if no number is entered
        {
            Console.WriteLine(input + " is an invalid number."); // error message
            int.TryParse(Console.ReadLine(), out numberTotal); // allows the user to input another value
        }

    } // this loop will repeat until numberTotal has an int set to 1 or above
Console.WriteLine(“您想输入多少数字?”);//请求号码
字符串输入=Console.ReadLine();//将输入设置为字符串变量
int numberTotal;//声明一个整型变量
if(!int.TryParse(input,out numberTotal))//处理输入是否为无效数字
{
while(numberTotal<1)//numberTotal在没有输入数字的情况下默认设置为0
{
Console.WriteLine(输入+”是无效数字。“);//错误消息
int.TryParse(Console.ReadLine(),out numberTotal);//允许用户输入另一个值
}
}//此循环将重复,直到numberTotal将int设置为1或更高
如果愿意,您也可以在FOR循环中使用上述方法,方法是不将操作声明为循环的第三个参数,例如

    Console.WriteLine("How many numbers do you want to enter?");
    string input2 = Console.ReadLine();

    if (!int.TryParse(input2, out numberTotal2))
    {
        for (int numberTotal2 = 0; numberTotal2 < 1;)
        {
            Console.WriteLine(input2 + " is an invalid number.");
            int.TryParse(Console.ReadLine(), out numberTotal2);
        }

    }
Console.WriteLine(“您想输入多少个数字?”);
字符串input2=Console.ReadLine();
如果(!int.TryParse(input2,out numberTotal2))
{
对于(int numberTotal2=0;numberTotal2<1;)
{
WriteLine(input2+“是一个无效的数字”);
int.TryParse(Console.ReadLine(),out numberTotal2);
}
}

如果不需要循环,只需删除整个循环大括号即可。此函数将告诉您字符串是否仅包含字符0123456789

private bool IsInt(string sVal)
{
    foreach (char c in sVal)
    {
         int iN = (int)c;
         if ((iN > 57) || (iN < 48))
            return false;
    }
    return true;
}
private bool IsInt(字符串sVal)
{
foreach(sVal中的字符c)
{
int iN=(int)c;
如果((iN>57)| |(iN<48))
返回false;
}
返回true;
}
这与int.TryParse()不同,int.TryParse()会告诉您字符串是否可以是整数。
例如,“123\r\n”将从int.TryParse()返回TRUE,但从上述函数返回FALSE


…取决于你需要回答的问题。

很简单。。。使用这段代码

bool anyname = your_string_Name.All(char.IsDigit);
如果字符串的整数为“其他方向的假”,则返回true。

对于Wil p解决方案(见上文),您也可以使用LINQ

var x = "12345";
var isNumeric = !string.IsNullOrEmpty(x) && x.All(Char.IsDigit);

我不久前写的东西。上面有一些很好的例子,但只值我2美分。

如果你只想检查它是否是字符串,你可以直接在方法调用中放置“out int”关键字。根据dotnetperls.com网站,旧版本的C#不允许这种语法。通过这样做,可以减少程序的行数

string x = "text or int";
if (int.TryParse(x, out int output))
{
  // Console.WriteLine(x);
  // x is an int
  // Do something
}
else
{
  // x is not an int
}
如果你还想得到int值,你可以这样写

方法1

string x = "text or int";
int value = 0;
if(int.TryParse(x, out value))
{
  // x is an int
  // Do something
}
  else
{
  // x is not an int
}
方法2

string x = "text or int";
int num = Convert.ToInt32(x);
Console.WriteLine(num);

参考:

也许这是另一种解决方案


也可以尝试以下方法:

    var ix=Convert.ToInt32(x);
    if (x==ix) //if this condition is met, then x is integer
    {
        //your code here
    }


你的代码中的
x
类型是什么?感谢编辑Holterman,我应该已经澄清了。是的,我想这真的取决于我最初将变量x声明为什么。这两个都不适用于我。当我看着它的时候。GetType()返回int64,但typeof(int)返回int32。我将假设第一个,如果有相同的事情,他们是否也有其他变量类型的TryParse方法?使用这种方法被认为是良好的编程实践吗?许多其他.NET原语类型(UInt64、Double等)也有
TryParse
方法。如果您有一个字符串,使用
TryParse
通常是将其转换为这些类型之一的最佳实践。@Alex,试试您的intellisense。它可以在您键入时显示每个类的每个成员。我仍然使用此方法得到负值。你能告诉我怎么忽略负值吗@Brandon@ArpitPatel负整数仍然是有效整数。如果您想忽略它,只需做一个
<0
检查。在TryParse上划掉性能注释,它只适用于int.Parse、Regex和char.IsNumber。我想到的参考资料出现在.NET2.0之前,当时还不存在TryParse。空字符串在此处被验证为数字,您应该以
isNumeric=false
开头。包含负值的字符串将不会被验证。这将检查以确保字符串的所有字符都是数字,但不处理负数、以(无运算)开头的数字
+
或周围的空白。(结果变量的名称也很混乱。)我也很想对它进行加密,因为它没有包括,例如
0.0
1000
(/
1.000
,如果您在某些地区),但TryParse也没有,所以我想它不会更糟。包含负值的字符串不会验证。是的,您是对的,这不是一个完整的整数测试。我只是简化了Wil P的建议。对于那些寻找正整数的简单测试的人来说,这是一个很好的解决方案,比如一个数字记录号。谢谢在不允许使用out参数的表达式树中,这可能很有用。
string x = "text or int";
int num = Convert.ToInt32(x);
Console.WriteLine(num);
try
{
    Console.Write("write your number : ");
    Console.WriteLine("Your number is : " + int.Parse(Console.ReadLine()));
}
catch (Exception x)
{
    Console.WriteLine(x.Message);
}
Console.ReadLine();
    var ix=Convert.ToInt32(x);
    if (x==ix) //if this condition is met, then x is integer
    {
        //your code here
    }