0

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

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

3 回答 3

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
于 2013-11-04T16:13:34.937 回答
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

于 2013-11-04T15:24:06.453 回答
0

NSRegular表达方式:

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


更新

性能测试NSPredicateNSRegularExpression

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 解决方案

于 2013-11-04T16:10:38.283 回答