views:

71

answers:

4

Hello,

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

A: 

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

withString:@""

It's missing for a few.

No one in particular
That was just my problem for typing in here. I fixed it. Do you know an efficient way to do this??? Thanks so much.
Christian Stewart
A: 

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.

rolls
Also a good answer. Thanks a lot :D
Christian Stewart
+1  A: 

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. :)

quixoto
Great comprehensive answer. I think I'll just go with this for now - I might get back to you about the custom function :D.
Christian Stewart
+3  A: 

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?
Richard
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!
Christian Stewart
The output was after the "method" was called, NSLog(@"%@", originalString);
Richard