开发者

Concatenating NSStrings in Objective C

开发者 https://www.devze.com 2022-12-19 13:31 出处:网络
How do I concatenate to NSStrings together in Objective开发者_如何学Python C?If the string is not mutable, you will instead want:

How do I concatenate to NSStrings together in Objective开发者_如何学Python C?


If the string is not mutable, you will instead want:

NSString *firstString = @"FirstString";
NSString *secondString = @"SecondString";
NSString *concatinatedString = [firstString stringByAppendingString:secondString];
// Note that concatinatedString is autoreleased, 
// so if you may want to [concaticatedString retain] it.

For completeness, here's the answer for a mutable string:

NSMutableString *firstString = [NSMutableString stringWithString:@"FirstString"];
NSString *secondString = @"SecondString";
[firstString appendString:secondString];
// Note that firstString is autoreleased, 
// so if you may want to [firstString retain] it.


If you have a mutable string then you can do:

NSMutableString* someString = [NSMutableString stringWithString: @"Hello"];
[someString appendString: @", world!"];

For example. Be more specific if this is not the answer you are looking for.

0

精彩评论

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