开发者

Strings in Objective C for Iphone

i am new to iphone application developmen开发者_运维知识库t. 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"


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];


Try do this:

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜