formating numbers from strings
-
-
i have a string that always is 6 chars long. If it is not then zeros are added to it. I am pullinging this info out of the database and i need to add 1 to it. Example: I get "000456" and need to add 1 to it so its "000457" Is there an easy way to do that?
Yes, try this:
string s = "000456";
int i = -1;
if (Int32.TryParse(s, out i))
{
i +=1;
s = String.Format("{0:000000}", i);
}This takes your string and tries to convert it to an integer value. If that succeeds, it adds 1 to it and then formats it back as a string to 6 characters padded with zeroes.
Scott Dorman
Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]
Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
-
i have a string that always is 6 chars long. If it is not then zeros are added to it. I am pullinging this info out of the database and i need to add 1 to it. Example: I get "000456" and need to add 1 to it so its "000457" Is there an easy way to do that?