将字符串转换为int C#

时间:2018-09-26 10:02:38

标签: c# .net string int typeconverter

我有下面的代码。

string inputStringg = "‪120000";
int numValuee = 0;
Console.WriteLine("Some Sample Name : " + numValuee.ToString());
try
{
    numValuee = Int32.Parse(inputStringg); // throws exception
}
catch (Exception ex)
{
    throw;
}
Console.WriteLine("Some Sample Name : " + numValuee.ToString());

string inputString1 = "120000";
int numValue1 = 0;
Console.WriteLine("Some Sample Name : " + numValue1.ToString());
try
{
    numValue1 = Int32.Parse(inputString1); // no exception here
}
catch (Exception ex)
{
    throw;
}

Console.WriteLine("Some Sample Name : " + numValue1.ToString());

这里numValuee = Int32.Parse(inputStringg);

引发异常Input string was not in a correct format.

并且此numValue1 = Int32.Parse(inputString1);不会引发异常。

我知道这是一个愚蠢的问题,但不知道发生了什么。

请有人指导。谢谢。

2 个答案:

答案 0 :(得分:2)

您是否从HTML页面复制了第一个字符串?我猜您复制的字符串具有隐藏字符(Unicode),可以在Word中查看它。但是,如果您确实在尝试try解析之后,我建议您执行以下操作:

string inputStringg = "‪120000";
inputStringg = new string(inputStringg.Where(c => char.IsLetterOrDigit(c)).ToArray());
int numvalue;
numvalue = int.Parse( inputStringg );
System.Diagnostics.Trace.WriteLine( "" + numvalue );

Unicode字符串可能包含控制字符和/或隐藏字符,因此在解析之前,我们需要对字符串进行相应的预处理。

答案 1 :(得分:1)

它们是不同的,您的第一个字符串包含一个不可见的字符,如已经提到的@Jon Skeet。这是基础的IL,由Ildasm

     // Code size       142 (0x8e)
  .maxstack  2
  .locals init ([0] string inputStringg,
           [1] int32 numValuee,
           [2] string inputString1,
           [3] int32 numValue1,
           [4] class [mscorlib]System.Exception ex,
           [5] class [mscorlib]System.Exception V_5)
  IL_0000:  nop
  IL_0001:  ldstr      bytearray (2A 20 31 00 32 00 30 00 30 00 30 00 30 00 )       // * 1.2.0.0.0.0.
  IL_0006:  stloc.0
  IL_0007:  ldc.i4.0
  IL_0008:  stloc.1
  IL_0009:  ldstr      "Some Sample Name : "
  IL_000e:  ldloca.s   numValuee
  IL_0010:  call       instance string [mscorlib]System.Int32::ToString()
  IL_0015:  call       string [mscorlib]System.String::Concat(string,
                                                              string)
  IL_001a:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_001f:  nop
  .try
  {
    IL_0020:  nop
    IL_0021:  ldloc.0
    IL_0022:  call       int32 [mscorlib]System.Int32::Parse(string)
    IL_0027:  stloc.1
    IL_0028:  nop
    IL_0029:  leave.s    IL_0030
  }  // end .try
  catch [mscorlib]System.Exception 
  {
    IL_002b:  stloc.s    ex
    IL_002d:  nop
    IL_002e:  rethrow
  }  // end handler
  IL_0030:  ldstr      "Some Sample Name : "
  IL_0035:  ldloca.s   numValuee
  IL_0037:  call       instance string [mscorlib]System.Int32::ToString()
  IL_003c:  call       string [mscorlib]System.String::Concat(string,
                                                              string)
  IL_0041:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_0046:  nop
  IL_0047:  ldstr      "120000"
  IL_004c:  stloc.2
  IL_004d:  ldc.i4.0
  IL_004e:  stloc.3
  IL_004f:  ldstr      "Some Sample Name : "
  IL_0054:  ldloca.s   numValue1
  IL_0056:  call       instance string [mscorlib]System.Int32::ToString()
  IL_005b:  call       string [mscorlib]System.String::Concat(string,
                                                              string)
  IL_0060:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_0065:  nop
  .try
  {
    IL_0066:  nop
    IL_0067:  ldloc.2
    IL_0068:  call       int32 [mscorlib]System.Int32::Parse(string)
    IL_006d:  stloc.3
    IL_006e:  nop
    IL_006f:  leave.s    IL_0076
  }  // end .try
  catch [mscorlib]System.Exception 
  {
    IL_0071:  stloc.s    V_5
    IL_0073:  nop
    IL_0074:  rethrow
  }  // end handler
  IL_0076:  ldstr      "Some Sample Name : "
  IL_007b:  ldloca.s   numValue1
  IL_007d:  call       instance string [mscorlib]System.Int32::ToString()
  IL_0082:  call       string [mscorlib]System.String::Concat(string,
                                                              string)
  IL_0087:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_008c:  nop
  IL_008d:  ret