C# 将字符串转换为int C
我有下面的代码C# 将字符串转换为int C,c#,.net,string,int,typeconverter,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(
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());
这里numvalue=Int32.ParseinputStringg
抛出异常输入字符串的格式不正确
这个numValue1=Int32.ParseinputString1;不抛出异常
我知道这是个愚蠢的问题,但不知道发生了什么
有人,请引导我。提前谢谢。您是否从HTML页面复制了第一个字符串?我猜您复制的字符串有隐藏的Unicode字符,可以在Word中查看。但是,如果您真的完成了try-parse工作,我可以建议您执行以下操作:
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字符串可能包含控制字符和/或隐藏字符,因此在解析之前,我们需要相应地预处理字符串 它们不一样,您的第一个字符串包含一个不可见的字符,如前所述。这是基础IL,通过:
第一个字符串包含一个不可见字符-从左到右嵌入。你应该找出为什么你的字符串中会出现这种情况,并加以修复;string code=string.Join,inputStringg.Selectc=>int c.ToStringx4;Debug.Writecodes;您将看到202a 0031 0032 0030 0030 0030 0030,而不是int numValue1=0;Console.WriteLineSome示例名称:+numValue1.ToString;尝试{numValue1=Int32.ParseinputString1;}捕获异常ex{throw;}您只需执行以下操作:int numValue1=0;int.TryParse inputString1,out numvalue 1;不,他们不是。我把你的问题中的每一个都抄了下来,看到了其中的区别。看看每一个的长度,你会发现它们的区别。现在,如果你说实际代码中的字符串与问题中的字符串不匹配,那么很难帮助你。
// 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