ios Objective C 中的简单字符串连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8853865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple string concatenation in Objective C
提问by Umair Khan Jadoon
I have an NSString named 'you' with value "This is a you string!".
我有一个名为“you”的 NSString,其值为“这是一个你的字符串!”。
I want to concat "123" in 'you', how can I do it?
我想在'你'中连接“123”,我该怎么做?
I am using this code and it is giving an error.
我正在使用此代码,但出现错误。
you=[you stringByAppendingString:@"123"];
回答by Vincent Bernier
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
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.
所以我的猜测是你you
是一个坏指针,nil
而不是你认为它拥有的 NSString 。
Have you try an NSLog on that value before the call?
您是否在通话前尝试对该值使用 NSLog?
回答by Spidey
You can try this also:
你也可以试试这个:
you = [NSString stringWithFormat:@"%@%@", you, @"123"];
回答by Jayprakash Dubey
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] 你:这是你的字符串!
[233:907] you : This is you String!123
[233:907] 你:这是你字符串!123
[233:907] you : This is you String!123
[233:907]你:这是你字符串!123
回答by Binoy jose
Please try this
请试试这个
NSString *version = @"14.5.1";
NSString *build = @"1.0";
self.versionLabel.text = [NSString stringWithFormat:@"%@%@%@%@%@" , @"V : " ,version,@" ( ",build, @" )" ];