C# 将字符串转换为整数

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

Convert String to Integer

c#string

提问by zack

Possible Duplicate:
how can I convert String to Int ?

可能的重复:
如何将 String 转换为 Int ?

Hi,

你好,

I have the following problem converting string to an integer:

我在将字符串转换为整数时遇到以下问题:

string str = line.Substring(0,1);

//This picks an integer at offset 0 from string 'line' 

So now string str contains a single integer in it. I am doing the following:

所以现在字符串 str 中包含一个整数。我正在做以下事情:

int i = Convert.ToInt32(str);

i should be printing an integer if I write the following statement right?

如果我写出以下语句,我应该打印一个整数吗?

Console.WriteLine(i);

It compiles without any error but gives the following error on runtime:

它编译时没有任何错误,但在运行时出现以下错误:

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

mscorlib.dll 中发生类型为“System.FormatException”的未处理异常

Additional information: Input string was not in a correct format.

附加信息:输入字符串的格式不正确。

Any help please?

请问有什么帮助吗?

采纳答案by Scott Dorman

Rather than using Convert.ToInt32(string)you should consider using Int32.TryParse(string, out int)instead. The TryParse methods are there to help deal with user-provided input in a safer manner. The most likely cause of your error is that the substring you are returning has an invalid string representation of an integer value.

与其使用Convert.ToInt32(string),不如考虑使用Int32.TryParse(string, out int)。TryParse 方法可帮助以更安全的方式处理用户提供的输入。导致错误的最可能原因是您返回的子字符串具有整数值的无效字符串表示形式。

string str = line.Substring(0,1);
int i = -1;
if (Int32.TryParse(str, out i))
{
   Console.WriteLine(i);
}

回答by Chris Thompson

It's entirely possible there is some whitespace in there. Try running something akin to trim() (I'm not sure what language you're in) that will strip the white space. Also, try printing out the string to make sure you actually have the right part of it. We've all done that :)

那里完全有可能有一些空白。尝试运行类似于 trim() 的东西(我不确定你使用的是什么语言),它会去除空白。此外,尝试打印出字符串以确保您确实拥有它的正确部分。我们都这样做了:)

回答by rahul

FormatException

value does not consist of an optional sign followed by a sequence of digits (0 through 9).

The exception that is thrown when the format of an argument does not meet the parameter specifications of the invoked method.

格式异常

value 不由可选符号后跟数字序列(0 到 9)组成。

当参数的格式不符合被调用方法的参数规范时抛出的异常。

You can use Int32.TryParseif you don't want to generate an exception like this.

如果您不想生成这样的异常,您可以使用Int32.TryParse

Int32.TryParse: Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.

Int32.TryParse:将数字的字符串表示形式转换为其等效的 32 位有符号整数。返回值指示操作是否成功。

回答by paxdiablo

It's likely that your input is nota valid format. Try this instead. If the number is not valid, it should output an error.

您的输入可能不是有效格式。试试这个。如果数字无效,则应输出错误。

Keep in mind that the string should consist of an optional sign followed by a number.

请记住,该字符串应由一个可选符号后跟一个数字组成。

string line = "23"; // or whatever.
string str = line.Substring(0,1);
int i = 0;
if (Int32.TryParse(str, out i)) {
   Console.WriteLine(i);
} else {
    Console.WriteLine ("Error converting '" + line + "', '" + str + "'.");
}

One thing you may be seeing is the user entering "-1"for example. If you do the substring(0,1)on that, you'll only get "-"which isn't really valid.

"-1"例如,您可能会看到的一件事是用户输入。如果你这样做substring(0,1),你只会得到"-"哪个不是真正有效的。

回答by StarSignLeo

Are you sure that the value returned in str is an int, set a debug point if your using visual studio. Ive got a feeling your problem maybe that your not actually returning an integer. Try:

您确定 str 中返回的值是一个 int,如果您使用的是 Visual Studio,请设置一个调试点。我有一种感觉,你的问题可能是你实际上没有返回一个整数。尝试:

line.Trim().Substring(0,1);

This will remove any whitespace.

这将删除任何空格。