Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C String-Replace

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

like image 404
Christian Stewart Avatar asked Apr 08 '26 08:04

Christian Stewart


2 Answers

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?
like image 198
Richard Avatar answered Apr 09 '26 20:04

Richard


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

like image 40
Ben Zotto Avatar answered Apr 09 '26 21:04

Ben Zotto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!