HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Replacing occurrences in NSString and NSMutableString

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
occurrencesnsstringreplacingandnsmutablestring

Problem

I ran it and it's working, but I just want someone to double check that I followed the directions. Any suggestions or corrections will be appreciated.

-

Assume you have a NSString *str object containing some text. Write a code fragment to create a new string with all substrings "aaa" deleted.

My answer:

NSString *str = @"aaa 123 yes there will be aaaa even more a y a y aaa, ";

NSString *str2 = [str stringByReplacingOccurrencesOfString:@"aaa" withString:@""];

NSLog(@"its %@", str2);


-

Redo when this is NSMutableString *str, without creating a new object.

My answer:

NSMutableString *string1 = [NSMutableString stringWithString: @"aaa 123 yes there aaaa even more a y a y aaa,"];

[string1 replaceOccurrencesOfString:@"aaa" withString:@"" options:0 range:NSMakeRange(0, string1.length)];
NSLog (@"string1 = %@", string1);
    }

Solution

In both cases, this is exactly the most efficient way for removing a substring from a string.

In practice, I'm not sure how practical or how frequently you'll truly need to be using the second example, and for most of us, simply doing this:

string1 = [[string1 stringByReplacingOccurrencesOfString:@"aaa" withString:@""] mutableCopy];


is a bit easier to remember. And you generally want to work with immutable objects whenever you can anyway. But given the requirements, the method you've used does remove the substring without creating a new object.

One thing that may or may not be of concern is the white space that's left in these strings at the beginning/end. So you may or may not want to put some effort into trimming the white space.

Also, by replacing @"aaa" with @"", that means you're also effectively replacing @"aaaa" with @"a". Be sure this is what is intended, otherwise this problem is a bit more complicated.

You can also use regex. Regex will be slightly less efficient, but can give you some more control over what's being replaced:

NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"/\baaa\b/" 
                                                                       options:nil
                                                                         error:&error];
NSString *str2 = [regex stringByReplacingMatchesInString:string 
                                                 options:nil   
                                                   range:NSMakeRange(0, [str length]) 
                                            withTemplate:@""];
NSLog(@"%@", str2);


I don't know at this moment whether there's anything similar to this available for mutating a NSMutableString object rather than creating a new object.

Code Snippets

string1 = [[string1 stringByReplacingOccurrencesOfString:@"aaa" withString:@""] mutableCopy];
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"/\baaa\b/" 
                                                                       options:nil
                                                                         error:&error];
NSString *str2 = [regex stringByReplacingMatchesInString:string 
                                                 options:nil   
                                                   range:NSMakeRange(0, [str length]) 
                                            withTemplate:@""];
NSLog(@"%@", str2);

Context

StackExchange Code Review Q#56292, answer score: 8

Revisions (0)

No revisions yet.