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

a 夏天 提交于 2019-12-04 18:10:56

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!