Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当字符串值可能为空时,将字符串数组值转换为int_C#_Null_Int_Arrays - Fatal编程技术网

C# 当字符串值可能为空时,将字符串数组值转换为int

C# 当字符串值可能为空时,将字符串数组值转换为int,c#,null,int,arrays,C#,Null,Int,Arrays,我无法将字符串数组中的值转换为int,因为该值可能为null StreamReader reader = File.OpenText(filePath); string currentLine = reader.ReadLine(); string[] splitLine = currentLine.Split(new char[] { '|' }); object.intValue = Convert.ToInt32(splitLine[10]); 除了splitLine[10]为空时,此功

我无法将字符串数组中的值转换为int,因为该值可能为null

StreamReader reader = File.OpenText(filePath);
string currentLine = reader.ReadLine();
string[] splitLine = currentLine.Split(new char[] { '|' });
object.intValue = Convert.ToInt32(splitLine[10]);
除了splitLine[10]为空时,此功能非常有效。
引发错误:`System.FormatException:输入字符串的格式不正确

有没有人能给我一些建议,告诉我处理这个问题的最佳方法是什么

if (splitLine[10] != null)
    object.intValue = Convert.ToInt32(splitLine[10]);
else
    //do something else, if you want
在获取
splitLine[10]
之前,您可能还需要检查
splitLine.Length>10

如果您正在读取类似CSV文件的内容,并且可能会有点复杂,例如读取多个值,那么使用连接字符串或其他库之类的东西来读取文件可能是有意义的。从中获取示例连接字符串,使用
分隔(|)
指定分隔符,然后像
使用(var conn=new OleDbConnection(connectionString))
一样使用它们。请参阅中关于使用喷气发动机的部分

在获取
splitLine[10]
之前,您可能还需要检查
splitLine.Length>10


如果您正在读取类似CSV文件的内容,并且可能会有点复杂,例如读取多个值,那么使用连接字符串或其他库之类的东西来读取文件可能是有意义的。从中获取示例连接字符串,使用
分隔(|)
指定分隔符,然后像
使用(var conn=new OleDbConnection(connectionString))
一样使用它们。请参阅中关于使用喷气发动机的部分。

不要使用convert,最好使用convert
int.TryParse()

e、 g


不要使用convert,最好使用
int.TryParse()

e、 g


您可以使用TryParse方法:

int value;
if(Int32.TryParse(splitLine[10], out value))
{
    object.intValue = value;
} 
else 
{
    // Do something with incorrect parse value
}

您可以使用TryParse方法:

int value;
if(Int32.TryParse(splitLine[10], out value))
{
    object.intValue = value;
} 
else 
{
    // Do something with incorrect parse value
}

如果您正在寻找最少的代码来编写,请尝试

object.intValue = Convert.ToInt32(splitLine[10] ?? "0");

如果要保留
splitLine[10]
中null的含义,则需要将
intValue
的类型更改为
Nullable
类型,然后可以为其分配null。这将代表更多的工作,但这是将空值与值类型(如整数)一起使用的最佳方法,无论您如何获得它们。

如果您希望编写的代码最少,请尝试

object.intValue = Convert.ToInt32(splitLine[10] ?? "0");
如果要保留
splitLine[10]
中null的含义,则需要将
intValue
的类型更改为
Nullable
类型,然后可以为其分配null。这将代表更多的工作,但这是将空值与值类型(如整数)一起使用的最佳方法,无论您如何获得它们。

我同意

object.intValue = int.Parse(splitLine[10] ?? "<int value you want>");
object.intValue=int.Parse(splitLine[10]?“”);
我会同意

object.intValue = int.Parse(splitLine[10] ?? "<int value you want>");
object.intValue=int.Parse(splitLine[10]?“”);


“更好”?对这些差异进行一些解释会很好。@TimS。如果字符串为
null
,或者
int
的格式不正确,则TryParse将返回
false
。Convert在这两种情况下都会抛出异常。
如果使用splitLine,则会得到异常。Length@Tim如果值不正确,它不会引发异常;如果不知道对象中是字符串还是装箱整数,则转换是好的,等等@L.B是的,根据他的示例,我假设总是这样,他问的是如何进行转换,而不是如何“更好地”处理数组本身?对这些差异进行一些解释会很好。@TimS。如果字符串为
null
,或者
int
的格式不正确,则TryParse将返回
false
。Convert在这两种情况下都会抛出异常。
如果使用splitLine,则会得到异常。Length@Tim如果值不正确,它不会引发异常;如果不知道对象中是字符串还是装箱整数,则转换是好的,等等@L.B是的,根据他的示例,我假设总是这样,当他询问如何进行转换,而不是如何处理数组本身时,如果splitLine[10]为空,是否会导致值为零?@Baxter是的,该运算符称为空合并运算符,“0”是字符串参数在
splitLine[10]时采用的默认值(扩展为
object.intValue
为null。我不同意这个答案,如何区分null合并为0的情况和splitLine[10]的实际值为0的情况……@EkoostikMartin我同意,特别是因为OP中的null似乎应该被忽略,而不是默认值。好吧,根据这个推理,OP需要将object.intValue的类型更改为可为null。很多工作。如果splitLine[10]为空,我怀疑操作是否会导致值为零?@Baxter是的,该运算符称为空合并运算符,“0”是字符串参数在
splitLine[10]
为空时将采用的默认值(扩展为您的
对象.intValue
)。我不同意这个答案,如何区分null合并为0的情况和splitLine[10]的实际值为0的情况……@EkoostikMartin我同意,特别是因为OP中的null似乎应该被忽略,而不是默认值。好吧,那么根据这种推理,OP需要将object.intValue的类型更改为可为null。很多工作。我怀疑这就是为什么不忽略空值
String.Split
允许您这样做。@Ramhound我不一定知道哪些值将为null,这会使我试图按字符串数组中的位置读取的其他项的[countnumber]偏离。实际问题是
splitLine[10]
是一个空字符串,而不是null。(
String.Split
从不返回空元素。)
Convert.ToInt32
如果其参数为空,则返回0