English 中文(简体)
核对表
原标题:Check NSString Contains Both Letters And Numbers

I m looking for the best way to check that an NSString contains both numerical and alphabetical characters. What I have come up with so far is the code below but this just tells me that no characters were entered which aren t numbers or letters.

if( [[myNSString stringByTrimmingCharactersInSet:
            [NSCharacterSet alphanumericCharacterSet]] isEqualToString:@""]){    
  //The string only contains alpha or numerical characters.
  //But now I want to check that both character sets are present ?    
}
最佳回答

页: 1 特征和特征 DigitCharacter 1. 如果制造的窒息与原体格不相同:

if (![[myOriginalString stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]] isEqualToString:myOriginalString] 
     && ![[myOriginalString stringByTrimmingCharactersInSet:[NSCharacterSet letterCharacterSet]] isEqualToString:myOriginalString]) {
...
}
问题回答

柯帕斯如果你在一开始或最后写过信件,在相反的末尾打上数字,那么他就会被罚款。 如果数字在信函或副手之间,例如bc123abc,就会出现问题。 这里的解决办法是:

if (![[[str componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""] isEqualToString:str]  
    && ![[[str componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@""] isEqualToString:str]) {  
    ....  
}

我确信,先前的两次答复都涵盖了这些要求,因此,这是对要求作出更严格解释的答案。

根据原员额守则中的评论意见要求:

//The string only contains alpha or numerical characters.   
//But now I want to check that both character sets are present?

这意味着,通过测试的扼杀必须只包括数字和信件,至少是其中之一。

www.un.org/Depts/DGACM/index_spanish.htm 如果属于某一组,则发现其属地,因此我们可以使用。

NSString *string = // ... string to test;

NSCharacterSet *illegalChars = [[NSCharacterSet alphanumericCharacterSet] invertedSet];

// Test if there is an illegal character in the string
BOOL hasIllegalCharacter = [string rangeOfCharacterFromSet: illegalCharacterSet].location != NSNotFound;

BOOL hasLetter = [string rangeOfCharacterFromSet: [NSCharacterSet letterCharacterSet]].location != NSNotFound;

BOOL hasDigit  = [string rangeOfCharacterFromSet: [NSCharacterSet decimalDigitCharacterSet]].location != NSNotFound;

if (hasDigit  && hasLetter  && !hasIllegalCharacter  ) {
    // String OK
}

如该表所详述,rangeOfCharacterFromSet: 返回NSRange of{NS NotFound, 0} 如果没有该编号的特性,则见插图。 这类似于在科拉进行扼杀的其他方法。





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签