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

自闭症网瘾萝莉.ら 提交于 2019-12-22 01:17:25

问题


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.



来源:https://stackoverflow.com/questions/5091851/how-to-detect-space-and-special-characters-like-etc-in-text-of-a-text

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