Reputation: 218
My intent is to analyze the user's input from a text field and check if he used any symbols.
For example, user's input is "/ardv/*k" and I need a variable that says the user typed 3 symbols.
I do not understand how to use NSRange to search against a string, because it stops as soon as it finds the first occurrence.
Also, NSSet is not compatible with characterAtIndex selector.
isEqualTo: didn't work.
Here's what I did, but I want to know how I can make this code simpler if possible:
...
NSString *testThisString = @"/ardv/*k";
NSCharacterSet *unknownFlags1 = [NSCharacterSet punctuationCharacterSet];
NSCharacterSet *unknownFlags2 = [NSCharacterSet symbolCharacterSet];
NSCharacterSet *unknownFlags3 = [NSCharacterSet whitespaceCharacterSet];
int count = 0;
for (int i = 0; i < [testThisString length]; i++) {
if ([unknownFlags1 characterIsMember:[testThisString characterAtIndex:i]] |
[unknownFlags2 characterIsMember:[testThisString characterAtIndex:i]] |
[unknownFlags3 characterIsMember:[testThisString characterAtIndex:i]]) {
count++;
}
}
NSLog(@"There are %d unknowns", count);
...
Upvotes: 0
Views: 1304
Reputation: 5173
To check if a string has any characters belonging to a set, you simply do this:
NSString *testString = @"hello$%^";
NSRange r = [testString rangeOfCharacterFromSet:[NSCharacterSet punctuationCharacterSet]];
if (r.location != NSNotFound) {
// the string contains a punctuation character
}
If you want to know all of the locations of the punctuation characters, just keep searching with a different input range. Something like:
NSRange searchRange = NSMakeRange(0, [testString length]);
while (searchRange.location != NSNotFound) {
NSRange foundRange = [searchString rangeOrCharacterFromSet:[NSCharacterSet punctuationCharacterSet] options:0 range:searchRange];
searchRange.location = foundRange.location + 1;
searchRange.length = [testString length] - searchRange.location;
if (foundRange.location != NSNotFound) {
// found a character at foundRange.location
}
}
Upvotes: 2
Reputation: 47729
Frankly, I'd just loop through the characters of the string, inspecting each individually, and running each through an if-ladder or some such to check for your "symbols". Would likely be faster than any kluge using, eg, rangeOfCharactersFromSet, and would be easier to understand and maintain.
Hint: Extract the character from the string only once, and put it in a unichar
temp, vs repeating characterAtIndex
multiple times. Faster and clearer.
Also, if the character set you're dealing with is ASCII (ie, character values of 127 and less) you can make up a bit or byte array to indicate the validity of each character. (You can even do this for wider characters, but the map array gets larger.) Not as elegant as NSCharacterSet, but you can fold all your criteria into one object. (You can also fold all the criteria into one NSCharacterSet, but it requires extracting the bitmaps and ORing them together.)
Upvotes: 0
Reputation: 21966
Also, NSSet is not compatible with characterAtIndex selector.
- (BOOL)characterIsMember:(unichar)aCharacter;
- (unichar)characterAtIndex:(NSUInteger)index;
They're both unichar's so you can do it this way.You're doing it well.
Upvotes: 0