views:

288

answers:

2

hello,

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 this??

+2  A: 

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:?]];
Johan Kool
A: 

(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];
KennyTM