Simple string concatenation in Objective C

霸气de小男生 提交于 2019-11-27 01:01:43

问题


I have an NSString named 'you' with value "This is a you string!".

I want to concat "123" in 'you', how can I do it?

I am using this code and it is giving an error.

you=[you stringByAppendingString:@"123"];

回答1:


This code here is working for me

NSString *s = @"avant";
s = [s stringByAppendingString:@" - après"];
NSLog(@"%@", s);

2012-01-13 11:48:59.442 tabbar[604:207] avant - après

So my guess is that your you is a bad pointer that is not nil and not the NSString you think it have.

Have you try an NSLog on that value before the call?




回答2:


You can try this also:

you = [NSString stringWithFormat:@"%@%@", you, @"123"];



回答3:


Code :

NSString *you;
you = @"This is you String!";
NSLog(@"you : %@ ",you);

you = [you stringByAppendingString:@"123"];
 NSLog(@"you : %@ ",you);

[you stringByAppendingFormat:@"%@%@",you,@"123"];
NSLog(@"you : %@ ",you);

Result in Console :

[233:907] you : This is you String!

[233:907] you : This is you String!123

[233:907] you : This is you String!123




回答4:


Please try this

NSString *version = @"14.5.1";

NSString *build = @"1.0";


self.versionLabel.text = [NSString stringWithFormat:@"%@%@%@%@%@" , @"V : " ,version,@" ( ",build, @" )" ];


来源:https://stackoverflow.com/questions/8853865/simple-string-concatenation-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!