TUNER88
TUNER88

Reputation: 923

Remove short words and characters from a string in Objective-C

I have a string which could be like this:

NSString *string = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry";

I want my result to be:

"Lorem Ipsum simply dummy printing typesetting industry"

My first idea:

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"some regex magic" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:@"Lorem Ipsum is simply dummy text of the printing and typesetting industry" options:0 range:NSMakeRange(0, [firstLine length]) withTemplate:@""];
NSLog(@"%@", modifiedString);

Upvotes: 0

Views: 656

Answers (3)

TUNER88
TUNER88

Reputation: 923

NSRegularExpression way:

NSString *text = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\b\\w{1,4}\\b\\s?" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:text options:0 range:NSMakeRange(0, [text length]) withTemplate:@""];
NSLog(@"Result: %@", modifiedString);

Result:

Lorem Ipsum simply dummy printing typesetting industry


UPDATE

Performance test NSPredicate vs NSRegularExpression

NSString *string = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry";

// NSPredicate
NSDate *start1 = [NSDate date];
for (int i = 1; i <= 10000; i++)
{
    NSArray *words = [string componentsSeparatedByString:@" "];
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"length > 4"];
    NSArray *largerWords = [words filteredArrayUsingPredicate:pred];
    NSString *filteredString = [largerWords componentsJoinedByString:@" "];
}
NSDate *finsh1 = [NSDate date];
NSTimeInterval executionTime1 = [finsh1 timeIntervalSinceDate:start1];
NSLog(@"Execution Time NSPredicate: %f", executionTime1);

// NSRegularExpression
NSDate *start2 = [NSDate date];
for (int i = 1; i <= 10000; i++)
{
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\b\\w{1,4}\\b\\s?" options:NSRegularExpressionCaseInsensitive error:&error];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@""];
}
NSDate *finsh2 = [NSDate date];
NSTimeInterval executionTime2= [finsh2 timeIntervalSinceDate:start2];
NSLog(@"Execution Time NSRegularExpression: %f", executionTime2);

Result:

Execution Time NSPredicate: 0.246003
Execution Time NSRegularExpression: 0.594555

NSPredicate solution is much faster than NSRegularExpression

Thanks @Alladinian for NSPredicate solution

Upvotes: 0

Alladinian
Alladinian

Reputation: 35616

Well I guess there must be a gazillion solutions to this. Here is one of them:

NSString *string = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry";
NSArray *words = [string componentsSeparatedByString:@" "];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"length > 4"];
NSArray *largerWords = [words filteredArrayUsingPredicate:pred];
NSString *filteredString = [largerWords componentsJoinedByString:@" "];

NSLog(@"%@", filteredString);
// Outputs => Lorem Ipsum simply dummy text printing typesetting industry

Upvotes: 3

Ilario
Ilario

Reputation: 6079

try (changing the >3 can decide the length of the word to be removed, in this case if you have less than 4 characters is removed):

 NSString *string = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry";

NSArray *words = [string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

NSMutableString *modified = [[NSMutableString alloc] init];
for(NSString *word in words){
    if([word length]>3){
        NSLog(@"Do something with big lengthy word: %@", word);
        [modified appendString:word];
        [modified appendString:@" "];
    }else{
        NSLog(@"This is a smaller word: %@", word);
    }

}
NSLog(@"Here is modified string : %@", modified);

output Here is modified string : Lorem Ipsum simply dummy text printing typesetting industry

Upvotes: 2

Related Questions