【问题标题】:Convert string to int C# [closed]将字符串转换为 int C# [关闭]
【发布时间】:2018-09-26 10:02:37
【问题描述】:

我有以下代码。

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); 不会抛出异常。

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

有人请指导我。提前谢谢。

【问题讨论】:

  • 您的第一个字符串包含一个不可见字符,U+202A - “从左到右嵌入”。你应该弄清楚为什么你的字符串中有这个,并修复它。
  • 为了帮助发现这类问题,我经常检查所涉及字符串的长度 - Console.WriteLine(inputStringg.Length);
  • string codes = string.Join(" ", inputStringg.Select(c => ((int) c).ToString("x4"))); Debug.Write(codes); 你会看到202a 0031 0032 0030 0030 0030 0030
  • 而不是int numValue1 = 0; Console.WriteLine("Some Sample Name : " + numValue1.ToString()); try { numValue1 = Int32.Parse(inputString1); } catch (Exception ex) { throw; } 你可以这样做:int numValue1 = 0; int.TryParse( inputString1, out numValue1 );
  • 不,他们不是。我将您的问题中的每一个都复制到csharpindepth.com/Articles/General/Unicode.aspx#explorer 并看到了不同之处。看看每个的长度,你应该会看到不同之处。现在,如果您说实际代码中的字符串与您的问题中的字符串不匹配,那将很难为您提供帮助。

标签: c# .net string int typeconverter


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    它们不一样,你的第一个字符串包含一个不可见的字符,如 @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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-11
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 2013-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多