开发者

append in middle of NSString in iPhone programming

开发者 https://www.devze.com 2022-12-24 10:22 出处:网络
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??\";

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

精彩评论

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

关注公众号