开发者

append in middle of NSString in iPhone programming

how to append the value of string in between NSString?

for example:

NSString* str1 = @"Hello";

NSString* str2 = @"Hi.."/*add contents of str1*/@"how r u??";

please tell me how to achieve t开发者_开发百科his??


There are multiple answers possible. It depends a bit on how you want to figure out where to insert the text. One possibility is:

NSString *outStr = [NSString stringWithFormat:"%@%@%@", [str2 substringToIndex:?], str1, [str2 substringFromIndex:?]];


(Append always means add to the end. That's inserting a string in the middle.)

If you simply want to construct a literal string, use

#define STR1 @"Hello"
NSString* str2 = @"Hi..." STR1 @" how r u??";

To insert it in run time you need to convert str2 into a mutable string and call -insertString:atIndex:.

NSMutableString* mstr2 = [str2 mutableCopy];
[mstr2 insertString:str1 atIndex:4];
return [mstr2 autorelease];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜