Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding numbers to a string?

Tags:

c#

I have strings that look like "01", "02". Is there an easy way that I can change the string into a number, add 1 and then change it back to a string so that these strings now look like "02", "03" etc. I'm not really good at C# as I just started and I have not had to get values before.

like image 746
JudyJ Avatar asked Dec 17 '22 14:12

JudyJ


1 Answers

To get from a string to an integer, you can youse int.Parse():

int i = int.Parse("07");

To get back into a string with a specific format you can use string.Format():

strings = string.Format("{0:00}",7);

The latter should give "07" if I understand http://www.csharp-examples.net/string-format-int/ correctly.

like image 101
Anders Abel Avatar answered Jan 02 '23 01:01

Anders Abel