如何在C#中将字符串转换为整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2344411/
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-07 01:38:53  来源:igfitidea点击:

How to convert string to integer in C#

c#.netstringinttype-conversion

提问by user282078

How do I convert a string to an integer in C#?

C#中如何将字符串转换为整数?

回答by Neil N

int a = int.Parse(myString);

or better yet, look into int.TryParse(string)

或者更好,看看 int.TryParse(string)

回答by devuxer

int myInt = System.Convert.ToInt32(myString);

As several others have mentioned, you can also use int.Parse()and int.TryParse().

正如其他几个人提到的,您也可以使用int.Parse()and int.TryParse()

If you're certain that the stringwill always be an int:

如果您确定string将始终是int

int myInt = int.Parse(myString);

If you'd like to check whether stringis really an intfirst:

如果您想检查是否string真的是int第一次:

int myInt;
bool isValid = int.TryParse(myString, out myInt); // the out keyword allows the method to essentially "return" a second value
if (isValid)
{
    int plusOne = myInt + 1;
}

回答by Daniel Mo?mondor

If you are sure that you have "real" number in your string, or you are comfortable of any exception that might arise, use this.

如果您确定您的字符串中有“真实”数字,或者您对可能出现的任何异常感到满意,请使用它。

string s="4";
int a=int.Parse(s);

For some more control over the process, use

要对过程进行更多控制,请使用

string s="maybe 4";
int a;
if (int.TryParse(s, out a)) {
    // it's int;
}
else {
    // it's no int, and there's no exception;
}

回答by Tomas Vana

Do something like:

做类似的事情:

var result = Int32.Parse(str);

回答by Brandon

If you're sure it'll parse correctly, use

如果您确定它会正确解析,请使用

int.Parse(string)

If you're not, use

如果不是,请使用

int i;
bool success = int.TryParse(string, out i);

Caution!In the case below, iwill equal 0, not 10 after the TryParse.

警告!在下面的情况下,i将等于 0,而不是 10 TryParse

int i = 10;
bool failure = int.TryParse("asdf", out i);

This is because TryParseuses an outparameter, not a refparameter.

这是因为TryParse使用的是out参数,而不是ref参数。

回答by madatanic

int i;
string whatever;

//Best since no exception raised
int.TryParse(whatever, out i);

//Better use try catch on this one
i = Convert.ToInt32(whatever);

回答by MadBoy

string varString = "15";
int i = int.Parse(varString);

or

或者

int varI;
string varString = "15";
int.TryParse(varString, out varI);

int.TryParseis safer since if you put something else in varString(for example "fsfdsfs") you would get an exception. By using int.TryParsewhen string can't be converted into int it will return 0.

int.TryParse更安全,因为如果你放入其他东西varString(例如“fsfdsfs”),你会得到一个例外。通过使用int.TryParsewhen string 无法转换为 int ,它将返回0.

回答by deepu

int i;

string result = Something;

i = Convert.ToInt32(result);

回答by Abdo

class MyMath
{
    public dynamic Sum(dynamic x, dynamic y)
    {
        return (x+y);
    }
}

class Demo
{
    static void Main(string[] args)
    {
        MyMath d = new MyMath();
        Console.WriteLine(d.Sum(23.2, 32.2));
    }
}

回答by Abdo

4 techniques are benchmarked here.

此处对 4 种技术进行了基准测试。

The fastest way turned out to be the following:

结果证明最快的方法如下:

y = 0;
for (int i = 0; i < s.Length; i++)
       y = y * 10 + (s[i] - '0');

"s" is your string that you want converted to an int. This code assumes you won't have any exceptions during the conversion. So if you know your string data will always be some sort of int value, the above code is the best way to go for pure speed.

“s”是您要转换为 int 的字符串。此代码假定您在转换过程中不会有任何异常。因此,如果您知道您的字符串数据将始终是某种 int 值,那么上面的代码是追求纯粹速度的最佳方式。

At the end, "y" will have your int value.

最后,“y”将具有您的 int 值。