从Objective-C中的字符串中删除短字和字符

时间:2013-11-04 15:15:12

标签: ios objective-c nsstring substring

我有一个字符串可能是这样的:

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

我希望我的结果是:

"Lorem Ipsum simply dummy printing typesetting industry"

我的第一个想法:

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);

3 个答案:

答案 0 :(得分:3)

嗯,我想一定有很多解决方案。这是其中之一:

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

答案 1 :(得分:2)

尝试(更改> 3可以决定要删除的单词的长度,在这种情况下,如果删除少于4个字符):

 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);

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

答案 2 :(得分:0)

NSRegularExpression方式:

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);

结果:

Lorem Ipsum simply dummy printing typesetting industry


更新

性能测试 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);

<强>结果:

Execution Time NSPredicate: 0.246003
Execution Time NSRegularExpression: 0.594555

NSPredicate解决方案比NSRegularExpression快得多

感谢@Alladinian提供NSPredicate解决方案