3

I want to replace multiple elements in my string in Objective-C.

In PHP you can do this:

str_replace(array("itemtoreplace", "anotheritemtoreplace", "yetanotheritemtoreplace"), "replacedValue", $string);

However in objective-c the only method I know is NSString replaceOccurancesOfString. Is there any efficient way to replace multiple strings?

This is my current solution (very inefficient and.. well... long)

NSString *newTitle = [[[itemTitleField.text stringByReplacingOccurrencesOfString:@"'" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@"'"] stringByReplacingOccurrencesOfString:@"^" withString:@""];

See what I mean?

Thanks, Christian Stewart

2
  • There's no equivalent to that PHP method in Obj-C. Try breaking your method call up into three lines so we can see exactly where the crash occurs. Commented Aug 30, 2010 at 1:24
  • Kubi, thanks for the answer. It's not really the crash that's bothering me. Its the inefficient-ness (if thats a word) of the whole deal. Thanks! Christian Commented Aug 30, 2010 at 1:28

4 Answers 4

12

If this is something you're regularly going to do in this program or another program, maybe make a method or conditional loop to pass the original string, and multi-dimensional array to hold the strings to find / replace. Probably not the most efficient, but something like this:

// Original String
NSString *originalString = @"My^ mother^ told me not to go' outside' to' play today. Why did I not listen to her?";

// Method Start
// MutableArray of String-pairs Arrays
NSMutableArray *arrayOfStringsToReplace = [NSMutableArray arrayWithObjects:
                                           [NSArray arrayWithObjects:@"'",@"",nil], 
                                           [NSArray arrayWithObjects:@" ",@"'",nil], 
                                           [NSArray arrayWithObjects:@"^",@"",nil], 
                                           nil];

// For or while loop to Find and Replace strings
while ([arrayOfStringsToReplace count] >= 1) {
    originalString = [originalString stringByReplacingOccurrencesOfString:[[arrayOfStringsToReplace objectAtIndex:0] objectAtIndex:0]
                                              withString:[[arrayOfStringsToReplace objectAtIndex:0] objectAtIndex:1]];
    [arrayOfStringsToReplace removeObjectAtIndex:0];
}

// Method End

Output:

2010-08-29 19:03:15.127 StackOverflow[1214:a0f] My'mother'told'me'not'to'go'outside'to'play'today.'Why'did'I'not'listen'to'her?
Sign up to request clarification or add additional context in comments.

3 Comments

Hey richard! AMAZING! Most detailed message I have ever seen. Just asking - what's the output? How did you get that? Looks totally like a legit output in the console - i mean - a0f?? Did you test this? Thanks so much!
The output was after the "method" was called, NSLog(@"%@", originalString);
Do we have to release the interim strings created?
2

There is no more compact way to write this with the Cocoa frameworks. It may appear inefficient from a code standpoint, but in practice this sort of thing is probably not going to come up that often, and unless your input is extremely large and you're doing this incredibly frequently, you will not suffer for it. Consider writing these on three separate lines for readability versus chaining them like that.

You can always write your own function if you're doing something performance-critical that requires batch replace like this. It would even be a fun interview question. :)

1 Comment

Great comprehensive answer. I think I'll just go with this for now - I might get back to you about the custom function :D.
0

Considered writing your own method? Tokenize the string and iterate through all of them replacing one by one, there really is no faster way than O(n) to replace words in a string.

Would be a single for loop at most.

Comments

-2

Add the @ to the start of the all the strings, as in

withString:@""

It's missing for a few.

1 Comment

That was just my problem for typing in here. I fixed it. Do you know an efficient way to do this??? Thanks so much.

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.