Add in the middle of NSString on iPhone

how to add string value between nsstring?

eg:

NSString* str1 = @"Hello";

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

Please tell me how to do this?

+3
source share
2 answers

(Adding always means adding to the end. This is the insertion of a line in the middle.)

If you just want to build a literal string, use

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

To insert it at runtime, you need to convert str2 to a mutable string and call -insertString:atIndex:.

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

There are several possible answers. It depends a little on how you want to figure out where to insert the text. One of the possibilities:

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

Source: https://habr.com/ru/post/1738444/


All Articles