【问题标题】:convert string to int error in c#在c#中将字符串转换为int错误
【发布时间】:2016-10-15 07:32:36
【问题描述】:

您好,我正在尝试将字符串转换为整数。

下面的代码显示了我尝试将字符串转换为整数的部分。

if (other.gameObject.CompareTag("PickUp"))
{
    if ( checkpointboolean == false)
    {
        string pickupName = other.ToString(); //other = Pickup4
        //Remove first 6 letters thus remaining with '4'
        string y = pickupName.Substring(6); 
        print(y); // 4 is being printed
        int x = 0;
        int.TryParse(y, out x);
        print (x); // 0 is being printed

我也尝试了下面的代码,而不是 '0' 我得到了以下错误:

if (other.gameObject.CompareTag("PickUp"))
{
    if ( checkpointboolean == false)
    {
        //Get Object name ex: Pickup4
        string pickupName = other.ToString();
        //Remove first 6 letters thus remaining with '4'
        string y = pickupName.Substring(6); 
        print(y);
        int x = int.Parse(y);

FormatException:输入字符串的格式不正确 System.Int32.Parse(System.String)

【问题讨论】:

  • 你有多确定 4之后没有字符?尝试打印y.Length...
  • 你必须用断点一步步调试它
  • 这似乎告诉您您的字符串包含无效字符。您确定该子字符串中只有 4 吗?如果您有一个调试器尝试查看该值,错过它然后更改使用 print("[" + y + "]");查看是否还有其他字符
  • 尝试修剪 y 以防有多余的间距。 ` int.TryParse(y.Trim(), out x);`
  • @Peter4499: Trim() 在上下文中没有用,因为默认 NumberStyles.Integer == NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign;见msdn.microsoft.com/en-us/library/…

标签: c# string integer


【解决方案1】:

int.TryParse 返回一个布尔值,如果成功则返回 true,否则返回 false。您需要将其包装在 if 块中并使用该逻辑执行某些操作。

if(int.TryParse(y, out x))
    print (x); // y was able to be converted to an int
else
    // inform the caller that y was not numeric, your conversion to number failed

至于为什么你的号码没有被转换,直到你发布字符串值是什么,我才能说出来。

【讨论】:

    【解决方案2】:

    首先,ToString() 通常用于调试目的所以不能保证

     other.ToString()
    

    将在软件的下一版本中返回预期的"Pickup4"。你可能想要类似的东西

      int x = other.gameObject.SomeProperty;
      int x = other.SomeOtherProperty;
      int x = other.ComputePickUp();
      ...
    

    然而,如果你坚持.ToString(),你宁愿不要硬编码六个字母(原因是一样的:调试信息往往会从版本变为版本),但使用正则表达式之类的:

      var match = Regex.Match(other.ToString(), "-?[0-9]+");
    
      if (match.Success) {
        int value; 
    
        if (int.TryParse(match.Value, out value))
          print(value); 
        else
          print(match.Value + " is not an integer value"); 
      }
      else
        print("Unexpected value: " + other.ToString());
    

    【讨论】:

      【解决方案3】:

      您的第一次尝试最适合这种情况,该代码工作正常,int.TryParse() 0 提供给 out 参数并返回 false 表示转换失败。输入字符串的格式不正确/无法转换为整数。您可以使用int.TryParse() 的返回值来检查这一点。为此,您需要做的是:-

       if(int.TryParse(y, out x))
           print (x); // 
       else
           print ("Invalid input - Conversion failed");
      

      【讨论】:

      • "int.TryParse() 输出为 0 表示转换失败。" - 实际上它表示 可能 失败。它也可能只是一个包含 0 的字符串。OP 应该正确检查返回码。
      • @CompuChip: 好吧,但不是“包含0的字符串”,如果输入是"0",它将是0
      猜你喜欢
      • 2018-01-13
      • 2011-07-11
      • 2013-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多