Quote:
Originally Posted by AndrewSpeaksOut
I'm trying to take the text typed or pasted into a searchbar and remove all punctuation and separate each individual word and place it in an array. This is as close as I've gotten but there are a lot of empty entries in the array where the punctuation was.
Code:
NSString *sourceData = source.text;
NSString *filteredData = [[sourceData componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@"_"];
NSArray *components = [filteredData componentsSeparatedByString:@"_"];
There must be a better way of doing this. I need a very efficient way.
|
You can do it this way which is much more efficient:
Code:
NSString *Source = @"Bla1;' Bla2#^ Bla3";
NSCharacterSet *AcceptedCharacterSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
Source = [[Source componentsSeparatedByCharactersInSet:AcceptedCharacterSet] componentsJoinedByString:@" "];
NSArray *Words = [Source componentsSeparatedByString:@" "];
int WordCounter = 0;
for (WordCounter = 0; WordCounter < [Words count]; WordCounter++){
NSLog(@"%@", [Words objectAtIndex:WordCounter]);
}
This outputs:
Bla1
Bla2
Bla3