Skip to main content
I -> In
Source Link
rolfl
  • 98k
  • 17
  • 219
  • 419

IIn 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.

I 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.

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.

added 931 characters in body
Source Link
nhgrif
  • 25.4k
  • 3
  • 64
  • 129

I 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.

I 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.

I 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.

Source Link
nhgrif
  • 25.4k
  • 3
  • 64
  • 129

I 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.