views:

65

answers:

5

i am adding a new record to xml file

im first quering all existing items and storing the count in an int

int number = query.count()

and then incrementing number by 1;

number = number +1;

now i want to format this value in a string having "N00000000" format and the number will ocuppy the last positions

Pseudo code:

//declare the format string
sting format = "N00000000"

//calculate the length of number string
int length =number.ToString().Length();

// delete as many characters from right to left as the length of number string
???

// finally concatenate both strings with + operator
???

help please

+3  A: 
int i = 123;
string n = "N" + i.ToString().PadLeft(8, '0');
Michael Buen
I went to the trouble of making my own extension method for this :( . Thanks for the tip.
Callum Rogers
+4  A: 
String output = "N" + String.Format ("00000000", length)

Alternatively if you change your formatstring to "'N'00000000" you can even use:

String output = String.Format (formatString, length)

Which means you can fully specify your output by changing your formatstring without having to change any code.

Foxfire
+2  A: 

You can use the built in ToString overload that takes a custom numeric format string:

string result = "N" + number.ToString("00000000");
Oded
+1  A: 

Here is a another one ...

result = String.Format("N{0:00000000}",number);
Harald Scheirich
oops got there before me :(
Sunny
+2  A: 
var result = number.ToString("N{0:0000000}");

HTH

Sunny