Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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.

  1. 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);
    
  2. 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);
        }
    
share|improve this question

1 Answer 1

up vote 7 down vote accepted

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.

share|improve this answer
    
I think you're right. I don't want @"aaaa" to be replaced with @"a". Can't get my head around it how to stop that from occurring. –  user3478869 Jul 7 at 1:04
1  
In that case, I think regex is what you need to look into, and that's something I'm not good with at all. –  nhgrif Jul 7 at 1:13
    
To match any number of 'a' sequences with more than three 'a' characters, use the following regex: a{3,}. This basically translates to "3 'a' characters with an arbitrary number of 'a' characters afterwards" –  mleyfman Jul 12 at 0:09
    
I think the intent is to replace exactly "aaa" only and ignore any other sequences. –  nhgrif Jul 12 at 0:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.