检查 NSString 是否包含特殊字符和数字

发布于 2024-11-24 16:03:26 字数 54 浏览 1 评论 0原文

我需要检查一个字符串是否包含一个大写字母,一个小写字母,一个整数和一个特殊字符。我该如何检查?

I need to check whether a string contains one uppercase letter, one lower case letter, one integer and one special character. How do I check?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

眼眸印温柔 2024-12-01 16:03:27

没有任何其他框架:

NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"] invertedSet];

if ([aString rangeOfCharacterFromSet:set].location != NSNotFound) {
    NSLog(@"This string contains illegal characters.");
}

您还可以使用正则表达式(此语法来自 RegexKitLite: http://regexkit.sourceforge.net< /a>):

if ([aString isMatchedByRegex:@"[^a-zA-Z0-9]"]) {
    NSLog(@"This string contains illegal characters.");
}

Without any additional frameworks:

NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"] invertedSet];

if ([aString rangeOfCharacterFromSet:set].location != NSNotFound) {
    NSLog(@"This string contains illegal characters.");
}

You could also use a regex (this syntax is from RegexKitLite: http://regexkit.sourceforge.net):

if ([aString isMatchedByRegex:@"[^a-zA-Z0-9]"]) {
    NSLog(@"This string contains illegal characters.");
}
云仙小弟 2024-12-01 16:03:27

找到了一个稍微好一点的实现。对 Ameesh 答案的改进

- (BOOL)isValidString:(NSString *)string
{
    return [string rangeOfCharacterFromSet:[NSCharacterSet uppercaseLetterCharacterSet]].location != NSNotFound &&
    [string rangeOfCharacterFromSet:[NSCharacterSet lowercaseLetterCharacterSet]].location != NSNotFound &&
    [string rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location != NSNotFound;
}

Found a slightly better implementation. Improvement on Ameesh's answer

- (BOOL)isValidString:(NSString *)string
{
    return [string rangeOfCharacterFromSet:[NSCharacterSet uppercaseLetterCharacterSet]].location != NSNotFound &&
    [string rangeOfCharacterFromSet:[NSCharacterSet lowercaseLetterCharacterSet]].location != NSNotFound &&
    [string rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location != NSNotFound;
}
后知后觉 2024-12-01 16:03:27

Maulik的答案是不正确的。这将检查除任何字母数字字符之外的任何内容,但不强制要求存在一个大写字母、一个小写字母和一个整数。事实上,您必须执行 3 次检查来验证每个约束。

NSCharacterSet *lowerCaseChars = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyz"];

NSCharacterSet *upperCaseChars = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLKMNOPQRSTUVWXYZ"];

NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];

if ([aString rangeOfCharacterFromSet:lowerCaseChars].location == NSNotFound || [aString rangeOfCharacterFromSet:upperCaseChars].location = NSNotFound || [aString rangeOfCharacterFromSet:numbers].location == NSNotFound) {
 NSLog(@"This string contains illegal characters");
}

Maulik's answer is incorrect. That will check for anything OTHER than any alphanumeric character, but doesn't enforce that there be one uppercase letter, one lowercase letter, and one integer. You do in fact have to do 3 checks to verify each constraint.

NSCharacterSet *lowerCaseChars = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyz"];

NSCharacterSet *upperCaseChars = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLKMNOPQRSTUVWXYZ"];

NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];

if ([aString rangeOfCharacterFromSet:lowerCaseChars].location == NSNotFound || [aString rangeOfCharacterFromSet:upperCaseChars].location = NSNotFound || [aString rangeOfCharacterFromSet:numbers].location == NSNotFound) {
 NSLog(@"This string contains illegal characters");
}
单身情人 2024-12-01 16:03:27

阿拉伯语/英语

NSString *regEx = @"^([a-zA-Z0-9\u0600-\u06ff](\\-|\\_|\\.|\\ )?)+$";
        NSPredicate *regExPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regEx];
        BOOL myStringMatchesRegEx = [regExPredicate evaluateWithObject: self.text];
        if (!myStringMatchesRegEx)
            return NO;

For Arabic/English

NSString *regEx = @"^([a-zA-Z0-9\u0600-\u06ff](\\-|\\_|\\.|\\ )?)+$";
        NSPredicate *regExPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regEx];
        BOOL myStringMatchesRegEx = [regExPredicate evaluateWithObject: self.text];
        if (!myStringMatchesRegEx)
            return NO;
九歌凝 2024-12-01 16:03:27

这就是我要做的。为您需要检查相应值是否存在的每个条件创建正则表达式。

即正则表达式检查它是否有一个大写字母,一个小写字母,一个整数和一个特殊字符,等等。

然后使用相同的字符串检查每个正则表达式,如果它们都返回 true,则您有获胜者,如果不是,则字符串与您的条件不匹配。

// 验证大写字母的示例对所有其他具有匹配正则表达式的示例执行相同的操作。

-(BOOL) validateOneUpperCaseLetter:(NSString *)string {
if ((string == nil) || ([string isEqualToString: @""])) {
    return NO;
}

// Change this regEx to one you needed. // this one validates for the "name".  
NSString *regEx = @"^[a-zA-Z]+(([\\'\\,\\.\\ -][a-zA-Z])?[a-zA-Z]\\s*)*$";
NSPredicate *regExPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regEx];
BOOL myStringMatchesRegEx = [regExPredicate evaluateWithObject: string];

if (!myStringMatchesRegEx) {
    return NO;
}
return YES;}

Here is what I would do. Create Regular expressions for every condition you need to check if corresponding value present or not.

i.e. Regular Expression to check if it has one uppercase letter, one lower case letter , one integer and one special character, and so on.

and then use the same string to check against every regular expression if all of them return true you have winner if not then string doesn't match to your criteria.

// Example for the Validating UpperCaseLetter do same for all others with matching regular expression.

-(BOOL) validateOneUpperCaseLetter:(NSString *)string {
if ((string == nil) || ([string isEqualToString: @""])) {
    return NO;
}

// Change this regEx to one you needed. // this one validates for the "name".  
NSString *regEx = @"^[a-zA-Z]+(([\\'\\,\\.\\ -][a-zA-Z])?[a-zA-Z]\\s*)*$";
NSPredicate *regExPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regEx];
BOOL myStringMatchesRegEx = [regExPredicate evaluateWithObject: string];

if (!myStringMatchesRegEx) {
    return NO;
}
return YES;}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文