Reputation: 49
i am new to iphone application development. i have a string say abc, i just want to display it as "Hello abc" in the screen
i want to add Hello to abc , before abc.
In objective c, i saw functions appendString , which displays result as "abc Hello"
But i want to display i as "Hello abc"
Upvotes: 1
Views: 112
Reputation: 8114
You dont need to re-declare the NString variables you can do it via single Mutable variable like.....
NSMutableString *myStr = [NSMutableString string];
[myStr appendString:@"Hello"];
[myStr appendString:@" Cloud"];
int num = 9;
[myStr appendFormat:@" Number %i",num];
NSLog(@"%@",myStr);
and this will print "Hello Cloud Number 9".
Hope this helps.
Upvotes: 0
Reputation: 13660
The easiest way to do it is:
NString *myString = @"abc";
NSString *finalString = [NSString stringWithFormat:@"Hello %@", myString];
NSLog(@"%@", finalString);
this will output "Hello abc".
I say that this is the easiest way, beacause you can reuse this method to add more stuff to the string, like a number:
NSString *playerName = @"Joe";
int num = 5;
[NSString stringWithFormat:@"%@ scored %d goals.", playerName, 5];
Upvotes: 3
Reputation: 33445
Try do this:
NSString *string1 = @"abc";
NSString *string2 = [NSString stringWithFormat:@"Hello %@", string1];
Upvotes: 1