【问题标题】:How can I convert a string to int? [duplicate]如何将字符串转换为 int? [复制]
【发布时间】:2012-07-04 06:58:16
【问题描述】:

可能重复:
How can I convert String to Int?

public List<int> GetListIntKey(int keys)
{
        int j;
        List<int> t;
        t = new List<int>();
        int i;
        for (i = 0; ; i++)
        {
            j = GetKey((keys + i).ToString());
            if (j == null)
            {
                break;
            }
            else
            {
                t.Add(j);
            }
        }
        if (t.Count == 0)
            return null;
        else
            return t;
}

问题就出来了:

j = GetKey((keys + i).ToString());

我收到错误说:

无法将类型'string'隐式转换为'int'

现在GetKey 函数是字符串类型:

public string GetKey(string key)
{
}

我该怎么办?

【问题讨论】:

  • This question询问关于将int转换为字符串,但您已经弄清楚了,错误消息是关于将字符串转换为int。
  • 请编辑您的问题并更正其标题。问题是关于字符串到 int 的转换,而不是 int 到字符串。
  • 我喜欢大多数正确答案被否决的方式。
  • 这是一个几乎重复的问题,有很多正确答案(还有很多反对票,然后是反对票)。这个问题肯定无法为 SO 添加价值,应该像@hvd 建议的那样关闭。

标签: c#


【解决方案1】:

问题在于“j”是一个 int,而您将它分配给 GetKey 的返回值。要么将 "j" 设为字符串,要么将 GetKey 的返回类型更改为 int。

【讨论】:

    【解决方案2】:

    试试这个:

    j = Int32.Parse(GetKey((keys + i).ToString()));
    

    如果值不是一个有效的整数,它会抛出一个异常。

    另一种方法是TryParse,如果转换不成功,它会返回一个布尔值:

    j = 0;
    
    Int32.TryParse(GetKey((keys + i).ToString()), out j);
    // this returns false when the value is not a valid integer.
    

    【讨论】:

      【解决方案3】:

      你的 getkey 的结果类型是 string 并且 j 变量被声明为 int

      解决办法是:

      j = Convert.ToInt32(GetKey((keys + i).ToString()));
      

      我希望这可以解决您的问题。

      【讨论】:

        【解决方案4】:

        试着改进你的代码,看看这个:

        public List<int> GetListIntKey(int keys)
        {
            var t = new List<int>();
        
            for (int i = 0; ; i++)
            {
                var j = GetKey((keys + i).ToString());
                int n;
                // check if it's possible to convert a number, because j is a string.
                if (int.TryParse(j, out n))
                    // if it works, add on the list
                    t.Add(n);
                else //otherwise it is not a number, null, empty, etc...
                    break;
            }
            return t.Count == 0 ? null : t;
        }
        

        希望对你有帮助! :)

        【讨论】:

          【解决方案5】:

          您收到错误是因为 GetKey 返回一个字符串,并且您试图将返回对象分配给声明为 int 的 j。您需要按照阿方索的建议将返回值转换为 int。你也可以使用:

          j = Convert.ToInt32(GetKey((keys+i).ToString()));
          

          【讨论】:

            【解决方案6】:

            您必须使用显式类型转换。

            使用

            int i = Convert.ToInt32(aString);
            

            转换。

            【讨论】:

            • 1.他想将 int 转换为字符串 2。这是一个 c# 问题。
            • @Femaref 重新。 1:不,他没有。问题标题错误,错误信息是关于字符串到整数的转换。
            • 我要快速查看标记的语言;)我已更正...
            【解决方案7】:
            What should i do ?
            

            你错了。了解值类型和引用类型。

            错误:

            1. 错误是Cannot implicitly convert type 'string' to 'int'。隐含的意思是它正在获取一个无法转换为 int 的字符串。 GetKeys 正在返回您尝试分配给整数 j 的字符串。

            2. 你的 j 是整数。如何用 null 进行检查。值类型什么时候可以为空?

            使用这个

            public List<int> GetListIntKey(int keys)
            {
                int j = 0;
                List<int> t = new List<int>();
                for (int i = 0; ; i++)
                {
                    string s = GetKey((keys + i).ToString());
            
                    if (Int.TryParse(s, out j))
                        break;
                    else
                        t.Add(j);
                }
            
                if (t.Count == 0)
                    return null;
                else
                    return t;
            }
            

            【讨论】:

            • “学习一些编程”是不必要且粗鲁的。我们都从某个地方开始,我们最不需要的就是在我们寻求帮助时有人贬低我们。
            • @DarvisLombardo:已更正。点。反向投反对票。
            • @daniloquio:我想要 +10。但我什至不明白。已达到我当天的当前限制 (200)。
            猜你喜欢
            • 2011-06-18
            • 2010-09-17
            • 1970-01-01
            • 1970-01-01
            • 2013-11-12
            • 2020-12-09
            • 2011-11-26
            • 1970-01-01
            • 2015-06-02
            相关资源
            最近更新 更多