How get the count of an array? inC#
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;
}
will u help me to correct this code... eg:(00001101) i need to add this no with 1. for that i want to convert this value开发者_开发问答 to int.if i convert to int the no will be(1101)+1 no will be (1102).after adding i want the answer (00001102).
how many zeros do you want?? You can use string.pad
int count = 1102;
int NumOfZeros = 10;
string s = count.ToString().PadLeft(NumOfZeros, '0');
there is also the number formatter.
count.ToString("D10");
String num = "000001101";
int item = int.Parse(num);
item++;
String output = item.ToString("D8");
You need to use String.Format("{0:00000000}", 1101);
, which would be 00001101
If you are storing this number as an int (and you should) 1102 annd 00001102 are the same thing. Use string formatting later when you need to output the value with some zeros.
1102.ToString("D8")
will give you the string "00001102"
Also, possible duplicates of this question: Pad with leading zeros
Try int.parseInt(startNo)
instead of converting it to a char
array.
精彩评论