-->

How to detect space, and special characters like :

2020-08-01 12:57发布

问题:

How to detect space, and special characters like :, ?,`,~, etc in text of a textfield in iPhone SDK?

回答1:

Try this..

NSCharacterSet* symbols = [NSCharacterSet symbolCharacterSet];
   if([symbols characterIsMember: yourCharacter]) {
        //Check Your condition here
    }

If you want to include many characters Use a combined NSCharacterset with NSMutableCharacterSet..

    NSMutableCharacterSet *space = [NSMutableCharacterSet characterSetWithCharactersInString:@" "];
    [space formUnionWithCharacterSet:[NSCharacterSet symbolCharacterSet]];

Use the space characterset to check the character



回答2:

You could also use an instance of NSScanner to parse your string. It is a bit more complicated than BuildSucceeded's answer but it's good to know that this exists.

Also when using NSScanner you would have to construct the right NSCharacterSet. Look in its documentation to find out how this works.