1

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".

1
  • 2
    Look at NSMutableString. Commented Nov 20, 2013 at 18:27

3 Answers 3

8

Why not just use an NSMutableString?

NSMutableString *string = [NSMutableString stringWithString:@"123456789"];

[string insertString:@"Hello" atIndex:5];

NSLog(@"%@", string);

Outputs:

12345Hello6789
Sign up to request clarification or add additional context in comments.

2 Comments

Why are you creating the mutable string from a C string? You can do string = [NSMutableString stringWithString:@"123456789"]` or string = [@"123456789" mutableString].
You're right, Im so used to seeing stringWithString telling me its redundant that I forgot to use it for this case. Edited the answer to reflect your comment.
2

You can use NSMutableString to accomplish this task. Specifically, see the reference to the insertString:atIndex: method which will do exactly what you want, i.e. insert a string into another string at a specified location. API LINK

Comments

1

you can implement this by using NSMutableString method insertString:atIndex:

Inserts into the receiver the characters of a given string at a given location.

- (void)insertString:(NSString *)aString atIndex:(NSUInteger)anIndex

Parameters

aString The string to insert into the receiver. aString must not be nil.

anIndex The location at which aString is inserted. The location must not exceed the bounds of the receiver.

Taken from apple developer classes ref

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.