【问题标题】:How get the count of an array? inC#如何获得数组的计数?在C#
【发布时间】:2011-03-03 04:56:58
【问题描述】:
char[] charArray = startno.ToCharArray();
//using this arry
//i want to cheque this 

int i=0;
count = 0;
while (chenum [i] != "0")
     {
         count++;
         i++;
     }
     string s = "0";
     string zero = "0";
     for (i = 1; i <= count; i++)
     {
         s = s + zero;
     }

你能帮我更正这段代码吗... 例如:(00001101) 我需要将此编号与 1 相加。 为此,我想将此值转换为 int。如果我转换为 int,则否将是(1101)+1 否将是(1102)。添加后我想要答案(00001102)。

【问题讨论】:

    标签: c# .net asp.net


    【解决方案1】:

    你想要多少个零??您可以使用 string.pad

    int count = 1102;
                int NumOfZeros = 10;
                string s = count.ToString().PadLeft(NumOfZeros, '0');
    

    还有数字格式化程序。

    count.ToString("D10");
    

    【讨论】:

    • PadLeft 非常漂亮。我从来没有注意到它。我一直使用字符串格式化程序。谢谢!
    【解决方案2】:
    String num = "000001101";
    int item = int.Parse(num);
    item++;
    String output = item.ToString("D8");
    

    【讨论】:

      【解决方案3】:

      您需要使用String.Format("{0:00000000}", 1101);,即00001101

      【讨论】:

        【解决方案4】:

        如果您将此数字存储为 int(并且应该),则 1102 和 00001102 是相同的。稍后当您需要输出带有一些零的值时使用字符串格式。

        1102.ToString("D8") 会给你字符串"00001102"

        另外,这个问题可能重复:Pad with leading zeros

        【讨论】:

        • 我想将它作为 00001102 存储在数据库中
        • @RAHUL: 是用作数字还是字符串?
        【解决方案5】:

        尝试int.parseInt(startNo),而不是将其转换为char 数组。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-04-09
          • 1970-01-01
          • 2018-04-01
          • 2017-02-11
          • 2022-01-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多