I wan't to "know" how to put a string at a location in another string. I already know because I figured out another way to do this. But I wan't to know the real way, does it even exist?
I'm also asking this question for future questions on how to put this string at a location in another string the "false" way (in case it can't be done the real way)
What I mean about putting a (sub)string at a location of string is for example to put
this string:@"Hello" at location:5 inString:@"123456789"
I want the results to be:@"12345Hello6789" Can this be done the real way? something like this fake code:
[str stringByPuttingString:@"s" atLocation:5];//this code does not exist
I figured out other ways to get this done, can we get it to shorter code?
-(NSString *)putString:(NSString *)str atLocation:(int)location ofString:(NSString *)mainString {
NSRange range = NSMakeRange(location, 0);
return [mainString stringByReplacingCharactersInRange:range withString:str];
}
and
-(NSString *)putString:(NSString *)str atLocation:(int)location ofString:(NSString *)mainString {
NSString *first = [mainString substringToIndex:location];
NSString *last = [mainString substringFromIndex:location];
return [NSString stringWithFormat:@"%@%@%@", first, str, last];
}
The first one feels best, any other ideas or real ideas?
Jonathan, in future cases of this "problem".
NSMutableString
. – rmaddy Nov 20 '13 at 18:27