Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        6年前关闭。
                                                                                            
                
        
我正在构造一个表单,并设法整理出表单中的输入。我要尝试做的是,当为邮政编码输入一个条目时,它将进行检查以确保其遵循预定格式。
到目前为止,我已经删除了空格,制作了2个字符集,其中一个带有数字值,另一个带有字母。然后检查一下字符串的长度。以下是:-

NSCharacterSet *cset = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"];
NSCharacterSet *nset = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"];

_QuestionAnswer.text = [_QuestionAnswer.text stringByReplacingOccurrencesOfString:@" " withString:@""]; //removes the space character
if (4 < _QuestionAnswer.text.length < 8) //checks string length between 5 and 7
{
    // this is where the format check should be done using cset and nset
}


NB UK邮政编码必须遵循以下任何一种示例格式:-

AA9A 9AA,

A9A 9AA,

A9 9AA,

A99 9AA,

AA9 9AA,

AA99 9AA,

其中'A'来自cset而'9'来自nset

先感谢您。

最佳答案

您可以使用正则表达式(我只是示例):

NSString *laxString = @".+@([A-Za-z0-9]+\\.)";
NSPredicate *postCodeTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", laxString];

return [postCode evaluateWithObject: postCodeTest];

关于ios - 检查邮政编码中的字符格式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19810761/

10-08 20:39