[SalesForce] How to add string values using apex

I have two string values using numbers. How to add(not concatenate) the two string values. Could you please help me.

my code:

string num='2016';
system.debug('first:'+num);
Output: 2016

string num1='2016'+'1';
system.debug('second:'+num1);
Output: 20161

But I need 2017.Is it possible or not this way?

Best Answer

Integer num = Integer.valueOf('2006');
Integer num2 = Integer.valueOf('1');

Integer result = num + num2;
System.debug(result);

Update as per comment, to transform it to a String:

String str = String.valueOf(result);
System.debug(str);
Related Topic