Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>NSRegularExpression way:</p> <pre><code>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:&amp;error]; NSString *modifiedString = [regex stringByReplacingMatchesInString:text options:0 range:NSMakeRange(0, [text length]) withTemplate:@""]; NSLog(@"Result: %@", modifiedString); </code></pre> <p>Result:</p> <pre><code>Lorem Ipsum simply dummy printing typesetting industry </code></pre> <p><br/> </p> <h1><strong>UPDATE</strong></h1> <p>Performance test <strong>NSPredicate</strong> vs <strong>NSRegularExpression</strong></p> <pre><code>NSString *string = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry"; // NSPredicate NSDate *start1 = [NSDate date]; for (int i = 1; i &lt;= 10000; i++) { NSArray *words = [string componentsSeparatedByString:@" "]; NSPredicate *pred = [NSPredicate predicateWithFormat:@"length &gt; 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 &lt;= 10000; i++) { NSError *error = NULL; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\b\\w{1,4}\\b\\s?" options:NSRegularExpressionCaseInsensitive error:&amp;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); </code></pre> <p><strong>Result:</strong></p> <pre><code>Execution Time NSPredicate: 0.246003 Execution Time NSRegularExpression: 0.594555 </code></pre> <p><strong>NSPredicate solution is much faster than NSRegularExpression</strong></p> <p>Thanks @Alladinian for NSPredicate solution</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload