2011-03-03 58 views
2
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.if我轉換爲int否(1101)+1否(1102)。添加後我想要答案(00001102)。如何獲取數組的數量? INC#

回答

1

嘗試int.parseInt(startNo)而不是將其轉換爲char陣列。

6

你要多少個零?你可以使用string.pad

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

還有數字格式化程序。

count.ToString("D10"); 
+0

PadLeft是非常漂亮的。我從未注意到它。我總是使用字符串格式化程序。謝謝! – 2011-03-03 05:10:39

2

如果你將這個數字存儲爲int(你應該)1102 annd 00001102是一樣的東西。稍後,當您需要使用一些零輸出值時,請使用字符串格式。

1102.ToString("D8")會給你字符串"00001102"

而且,這個問題可能的重複:Pad with leading zeros

+0

我想將它作爲00001102 – RAHUL 2011-03-03 05:09:53

+0

存儲在數據庫中@RAHUL:它是用作數字還是用作字符串? – Dyppl 2011-03-03 05:10:57

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

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