在Objective C中连接NSStrings

时间:2010-02-10 01:58:48

标签: iphone objective-c

如何在Objective C中连接到NSStrings

2 个答案:

答案 0 :(得分:22)

如果字符串不可变,则需要:

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.

为了完整性,这里是可变字符串的答案:

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.

答案 1 :(得分:9)

如果你有一个可变的字符串,那么你可以这样做:

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

例如。如果这不是您正在寻找的答案,请更具体。