Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate integers and strings in Objective C

Please forgive the simplicity of the question. I'm completely new to Objective C.

I'd like to know how to concatenate integer and string values and print them to the console.

This is what I'd like for my output:

10 + 20 = 30

In Java I'd write this code to produce the needed results:

System.Out.Println(intVarWith10 + " + " + intVarWith20 + " = " + result);

Objective-C is quite different. How can we concatenate the 3 integers along with the strings in between?

like image 242
Zolt Avatar asked Jan 15 '23 12:01

Zolt


2 Answers

You can use following code

int iFirst,iSecond;
iFirst=10;
iSecond=20;
NSLog(@"%@",[NSString stringWithFormat:@"%d + %d =%d",iFirst,iSecond,(iFirst+iSecond)]);
like image 157
svrushal Avatar answered Jan 25 '23 18:01

svrushal


Take a look at NSString - it has a method stringWithFormat that does what you require. For example:

NSString* yString = [NSString stringWithFormat:@"%d + %d = %d",
                              intVarWith10, intVarWith20 , result];
like image 28
ColinE Avatar answered Jan 25 '23 18:01

ColinE