如何从UIPasteBoard检查NSString格式

时间:2013-08-15 21:31:39

标签: ios nsstring uipasteboard

我允许用户从我的网站复制注册码,NSStirng然后保存到剪贴板,当应用程序打开注册对话框时,我立即使用此代码检查剪贴板 -

NSString *registrationCode = [UIPasteboard generalPasteboard].string;

现在我有了粘贴板的价值,我想检查它的格式..这应该是类似 AAAA-AAAA-AAAA-AAAA 的东西所以在essance中没有数字,没有空格(包括at正面和结尾),没有小写,没有特殊字符......只是大写字符。我怎么能得到这个?

1 个答案:

答案 0 :(得分:0)

尝试在您的代码中执行此操作:

NSCharacterSet * everythingThatsAllowedSet = [NSCharacterSet characterSetWithCharactersInString: @"ABCDEFGHIJKLMNOPQRSTUVWXYZ-"];
NSCharacterSet * characterSetFromRegistrationCode = [NSCharacterSet characterSetWithCharactersInString: registrationCode];

if([everythingThatsAllowedSet isSupersetOfSet: characterSetFromRegistrationCode ] == YES)
    NSLog( @"this is a valid registration code");
else
{
    // if there are any characters in the registration code that aren't members of
    // the everythingAllowed set, the "isSupersetOfSet" test will fail
    NSLog( @"this has invalid characters");
}