2013-11-04 118 views
0

刪除短字和字符我有可能是這樣的字符串:從字符串在Objective-C

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

多短? – fengd

+1

發佈您到目前爲止所嘗試的內容。 – rmaddy

+0

@fengd這應該是可定製的 – TUNER88

回答

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 


UPDATE

性能測試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解決方案

2

嘗試(更改> 3可以決定要刪除的單詞的長度,在這種情況下,如果您有少於4個字符是remov編):

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

+1

爲什麼追加一個空字符串,如果單詞被忽略? – rmaddy

+0

有沒有更清晰的方法來解決這個問題?也許與正則表達式? – TUNER88

+0

@rmaddy小錯誤,我修改 – Ilario

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