tags:

views:

62

answers:

3

Hi , 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"

+3  A: 

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];
Dimitris
+1  A: 

Try do this:

NSString *string1 = @"abc";
NSString *string2 = [NSString stringWithFormat:@"Hello %@", string1];
rein
A: 

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.

Madhup