Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding 1 to 0001 vb.net

Tags:

vb.net

I have a char array with a length of 4. I am attempting to create a way to add 1 to this char array and produce a string "0002".

I can convert the char array to an integer in order to add 1 to it, but the issue is I am losing the leading zeros in the process. Is there a way to preserve the zeros in the addition?

Some examples of output I want.. if endCode is "0009" and I add 1, I want "0010" if endCode is "0999" and I add 1, I want "1000"

I am losing the zeros with my current code..

(Convert.ToInt32(New String(endCode)) + j).ToString
like image 829
Nick LaMarca Avatar asked Apr 16 '26 08:04

Nick LaMarca


1 Answers

You're so close. Only 5 characters short. You just need a format string:

(Convert.ToInt32(New String(endCode)) + j).ToString("D4")

Though one wonders why or how this data came to be a character array in the first place.

like image 52
Joel Coehoorn Avatar answered May 01 '26 20:05

Joel Coehoorn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!