RAHUL
RAHUL

Reputation: 127

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).

Upvotes: 2

Views: 1384

Answers (5)

Dyppl
Dyppl

Reputation: 12381

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

Upvotes: 2

Kumar
Kumar

Reputation: 1015

You need to use String.Format("{0:00000000}", 1101);, which would be 00001101

Upvotes: 2

John Arlen
John Arlen

Reputation: 6689

String num = "000001101";
int item = int.Parse(num);
item++;
String output = item.ToString("D8");

Upvotes: 2

Dustin Davis
Dustin Davis

Reputation: 14585

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");

Upvotes: 6

mellamokb
mellamokb

Reputation: 56769

Try int.parseInt(startNo) instead of converting it to a char array.

Upvotes: 1

Related Questions